mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-16 16:50:41 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
61dfb2edf5 | ||
|
|
d4483e2bbd | ||
|
|
0de74566a9 | ||
|
|
edee53a022 | ||
|
|
fd3caf0d15 | ||
|
|
ce7906510f | ||
|
|
73462ee57f | ||
|
|
bc683b3cef | ||
|
|
94039c092a | ||
|
|
03f611811c | ||
|
|
1b7ec0a94f | ||
|
|
89d209e7df | ||
|
|
b5cc5ed736 | ||
|
|
15bb35573d | ||
|
|
6dda2e16a4 | ||
|
|
a01fd39f06 | ||
|
|
46589b3fac | ||
|
|
708d9de2de | ||
|
|
ebe6a7e19a | ||
|
|
09f7169f62 | ||
|
|
9125e51350 | ||
|
|
0da79a45eb | ||
|
|
d461ef9bed | ||
|
|
f123f0ba05 | ||
|
|
90c33e173c | ||
|
|
ac0f2bf2c6 | ||
|
|
8a8a4c165f | ||
|
|
7beb3aecf0 | ||
|
|
26d4812480 | ||
|
|
49edf1d389 | ||
|
|
ddbc75bbf6 | ||
|
|
9fbe7d63c5 | ||
|
|
3a78491f25 | ||
|
|
fc8c10cde9 | ||
|
|
125baa8c8b | ||
|
|
c5aa238f73 | ||
|
|
3673d37c1e | ||
|
|
59a45e092b | ||
|
|
a870f7d041 | ||
|
|
367be30a0f | ||
|
|
b66c0da9e1 | ||
|
|
ebef246486 | ||
|
|
fe991c4a7d | ||
|
|
5b248c24ec | ||
|
|
e05fd44f22 | ||
|
|
2355790e17 | ||
|
|
dd5d6a6c13 | ||
|
|
825961e1c2 | ||
|
|
bf0aa6879a | ||
|
|
078ca026a5 | ||
|
|
25d8d0c915 | ||
|
|
1a3348dfe9 |
No files matched your search
@@ -18,3 +18,31 @@ jobs:
|
|||||||
- uses: dtolnay/rust-toolchain@stable
|
- uses: dtolnay/rust-toolchain@stable
|
||||||
- uses: Swatinem/rust-cache@v2
|
- uses: Swatinem/rust-cache@v2
|
||||||
- run: cargo test --locked
|
- run: cargo test --locked
|
||||||
|
|
||||||
|
test-wayland:
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Install build dependencies (X11 + Wayland + portal)
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install -y build-essential pkg-config \
|
||||||
|
libx11-dev libxext-dev libxtst-dev libxfixes-dev libgtk-3-dev \
|
||||||
|
libwayland-dev libxkbcommon-dev libglib2.0-dev libpipewire-0.3-dev
|
||||||
|
- uses: dtolnay/rust-toolchain@stable
|
||||||
|
- uses: Swatinem/rust-cache@v2
|
||||||
|
- run: cargo test --locked --features wayland,portal
|
||||||
|
|
||||||
|
test-inproc:
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Install build dependencies (X11 + FFmpeg 6 for the inproc-av feature)
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install -y build-essential pkg-config \
|
||||||
|
libx11-dev libxext-dev libxtst-dev libxfixes-dev libgtk-3-dev \
|
||||||
|
libavcodec-dev libavformat-dev libavdevice-dev libswscale-dev libclang-dev
|
||||||
|
- uses: dtolnay/rust-toolchain@stable
|
||||||
|
- uses: Swatinem/rust-cache@v2
|
||||||
|
- run: cargo test --locked --features inproc-av
|
||||||
@@ -26,6 +26,7 @@ jobs:
|
|||||||
name=uu-remote-linux-${GITHUB_REF_NAME}-x86_64-linux
|
name=uu-remote-linux-${GITHUB_REF_NAME}-x86_64-linux
|
||||||
mkdir -p "$name"
|
mkdir -p "$name"
|
||||||
cp target/release/uuremote-server LICENSE README.md README.en.md config.example.json "$name/"
|
cp target/release/uuremote-server LICENSE README.md README.en.md config.example.json "$name/"
|
||||||
|
cp -r contrib "$name/"
|
||||||
tar czf "$name.tar.gz" "$name"
|
tar czf "$name.tar.gz" "$name"
|
||||||
sha256sum "$name.tar.gz" > "$name.tar.gz.sha256"
|
sha256sum "$name.tar.gz" > "$name.tar.gz.sha256"
|
||||||
- uses: softprops/action-gh-release@v2
|
- uses: softprops/action-gh-release@v2
|
||||||
|
|||||||
@@ -1,3 +1,8 @@
|
|||||||
target/
|
target/
|
||||||
*.log
|
*.log
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
|
# Internal QA records and handoffs stay private (kept under the parent project analysis/)
|
||||||
|
/docs/validation/
|
||||||
|
/docs/ACTIVE_HANDOFF.md
|
||||||
|
/contrib/tests/
|
||||||
@@ -36,7 +36,9 @@ Dependencies (Ubuntu/Debian):
|
|||||||
|
|
||||||
```sh
|
```sh
|
||||||
sudo apt install build-essential pkg-config \
|
sudo apt install build-essential pkg-config \
|
||||||
libx11-dev libxext-dev libxtst-dev libxfixes-dev libgtk-3-dev ffmpeg
|
libx11-dev libxext-dev libxtst-dev libxfixes-dev libavcodec-dev libavformat-dev libavdevice-dev libswscale-dev libclang-dev libgtk-3-dev ffmpeg
|
||||||
|
# --features inproc-av: libavcodec-dev libavformat-dev libavdevice-dev libswscale-dev libclang-dev
|
||||||
|
# (FFmpeg 6.x; in-process capture/encoding, Ubuntu 24.04 ships it)
|
||||||
# --features wayland: libwayland-dev libxkbcommon-dev wl-clipboard
|
# --features wayland: libwayland-dev libxkbcommon-dev wl-clipboard
|
||||||
# --features wayland,portal: + libglib2.0-dev libpipewire-0.3-dev
|
# --features wayland,portal: + libglib2.0-dev libpipewire-0.3-dev
|
||||||
```
|
```
|
||||||
@@ -102,11 +104,16 @@ viewer does not reconnect them automatically.
|
|||||||
|---|---|
|
|---|---|
|
||||||
| `~/.config/uuremote/config.json` | user_id, device_id, client_id, system_id, machine_guid, token, csrf; optional `sign_key`, `ffmpeg_root`. `$UU_CONFIG` overrides the path |
|
| `~/.config/uuremote/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 |
|
| `~/.config/uuremote/host_settings.json` | persisted 安全 settings: auto_lock, keep_mute, mute_permanent |
|
||||||
| `UU_DESKTOP_BACKEND` | `auto` (default) / `x11` / `wayland` / `portal` |
|
| `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_VIDEO_ENCODER=cpu` | force libx264 instead of NVENC |
|
| `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) |
|
||||||
|
| Mac viewer Cmd | With 「使用 macOS 快捷键」 off the Mac viewer sends Cmd=VK_LWIN 0x5B, Option=0xA4, Ctrl=0xA2; connect header `controller_platform`=4 is macOS. For Mac controllers Cmd is held back: Cmd+C/V/A/F/X/Z → Ctrl+key (terminal: Cmd+C/V → Ctrl+Shift+C/V), other combos/alone → Super |
|
||||||
|
| `UU_TERMINAL_SHORTCUTS=0` | disable Ctrl+V → Ctrl+Shift+V in focused X11 terminals (Mac Cmd and Ctrl both arrive as VK_LCONTROL) |
|
||||||
|
| `contrib/virtual-desktop/` | Xvfb + session (`UU_VIRTUAL_SESSION`, `UU_VIRTUAL_BUS=user|private`) for monitorless hosts; serve it with `DISPLAY=:99` |
|
||||||
|
| `UU_VIDEO_ENCODER` | `cpu`: libx264; `vaapi`: explicit Intel/AMD; `nvenc`: force; unset: NVENC→CPU only if `/proc/driver/nvidia/version` or `/dev/nvidia0` exists, else CPU (#10) |
|
||||||
| `UU_FFMPEG_ROOT` | FFmpeg prefix (`bin/ffmpeg`, libs in `lib/x86_64-linux-gnu`) |
|
| `UU_FFMPEG_ROOT` | FFmpeg prefix (`bin/ffmpeg`, libs in `lib/x86_64-linux-gnu`) |
|
||||||
| `UU_AUDIO=0`, `UU_AUDIO_SOURCE` | disable sound / choose the Pulse source (default `@DEFAULT_MONITOR@`) |
|
| `UU_AUDIO=0`, `UU_AUDIO_SOURCE` | disable sound / choose the Pulse source (default `@DEFAULT_MONITOR@`) |
|
||||||
| `UU_LOGIN_WINDOW=0` | don't auto-open the login window |
|
| `UU_LOGIN_WINDOW=0` | don't auto-open the login window |
|
||||||
|
| `UU_REMOTE_ASSIST=0`, `UU_ASSIST_ALLOW=1`, `UU_ASSIST_UI=tty`, `UU_ASSIST_DEBUG=1` | disable assistance / allow at start / terminal UI for `assist` / include the server's short error message in assist errors |
|
||||||
|
|
||||||
Token has no expiry field; API code 1120 (HTTP 422) means invalid/expired → re-login. The config is re-read
|
Token has no expiry field; API code 1120 (HTTP 422) means invalid/expired → re-login. The config is re-read
|
||||||
every 60 s.
|
every 60 s.
|
||||||
@@ -121,7 +128,7 @@ every 60 s.
|
|||||||
| `src/webrtc.rs` | peer connection, data channels, CONTROL dispatch, initialization messages |
|
| `src/webrtc.rs` | peer connection, data channels, CONTROL dispatch, initialization messages |
|
||||||
| `src/video.rs`, `src/capture_setting.rs` | FFmpeg H.264 pipeline; viewer quality/fps/resolution settings |
|
| `src/video.rs`, `src/capture_setting.rs` | FFmpeg H.264 pipeline; viewer quality/fps/resolution settings |
|
||||||
| `src/audio.rs` | Pulse/PipeWire monitor → Opus audio track |
|
| `src/audio.rs` | Pulse/PipeWire monitor → Opus audio track |
|
||||||
| `src/input.rs`, `src/input_mapping.rs` | input injection (X11 XTest / Wayland / portal), Windows VK → keysym |
|
| `src/input.rs`, `src/input_mapping.rs`, `src/uinput.rs` | input injection (X11 XTest / Wayland / portal / uinput), Windows VK → keysym → evdev |
|
||||||
| `src/cursor.rs` | pointer shape mirroring (X11) |
|
| `src/cursor.rs` | pointer shape mirroring (X11) |
|
||||||
| `src/clipboard*.rs` | text clipboard bridge and protocol |
|
| `src/clipboard*.rs` | text clipboard bridge and protocol |
|
||||||
| `src/file_trans.rs` | FileTransferFtp RPC |
|
| `src/file_trans.rs` | FileTransferFtp RPC |
|
||||||
@@ -133,7 +140,7 @@ every 60 s.
|
|||||||
| `src/desktop_backend.rs`, `src/wayland.rs`, `src/portal*.rs` | backend selection, Hyprland, GNOME/KDE portal |
|
| `src/desktop_backend.rs`, `src/wayland.rs`, `src/portal*.rs` | backend selection, Hyprland, GNOME/KDE portal |
|
||||||
| `c_engine/x11_engine.c`, `c_engine/clipboard.c` | X11 capture/input/selection |
|
| `c_engine/x11_engine.c`, `c_engine/clipboard.c` | X11 capture/input/selection |
|
||||||
| `c_engine/wayland_engine.c`, `c_engine/portal_engine.c` | Wayland and portal engines |
|
| `c_engine/wayland_engine.c`, `c_engine/portal_engine.c` | Wayland and portal engines |
|
||||||
| `c_engine/login_window.c` | GTK3 login window |
|
| `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),
|
Diagnostic subcommands: `login [--no-gui]`, `unlock-check` (password on stdin, prints only accepted/rejected),
|
||||||
`desktop-probe [new.png]`, `desktop-video-probe`, `wayland-input-probe`, `clipboard-acceptance`,
|
`desktop-probe [new.png]`, `desktop-video-probe`, `wayland-input-probe`, `clipboard-acceptance`,
|
||||||
@@ -172,11 +179,35 @@ started a second full host server instead, so check that a subcommand exists in
|
|||||||
`operation_result`. Host root is shown as ":/" (此电脑); home is "~/".
|
`operation_result`. Host root is shown as ":/" (此电脑); home is "~/".
|
||||||
- Terminal: BINARY channel, RPC 28 check_env → 24; frames `"TERM"`, version 1, type (enum+1), 2 flag bytes,
|
- Terminal: BINARY channel, RPC 28 check_env → 24; frames `"TERM"`, version 1, type (enum+1), 2 flag bytes,
|
||||||
session u32 LE, length u32 LE, payload; JSON `{cols,rows,shell,option}`.
|
session u32 LE, length u32 LE, payload; JSON `{cols,rows,shell,option}`.
|
||||||
|
- Port mapping (capture_type 9): Message 27 `PortMappingFrame {1 session_id, 2 rule_id, 3 stream_id, 4 type, 5 payload}`
|
||||||
|
on FILE_DATA_CHANNEL only. Types SYN=0, FIN=1, DATA=2, SYN_ACK=3, DATA_ACK=4 (ignored by both sides). SYN payload
|
||||||
|
JSON `{"version","target_host","target_port"}` → connect → SYN_ACK `{"version","ok","error"}`; DATA raw bytes both ways.
|
||||||
|
Echo session/rule/stream ids with their original wire types. The viewer does not support TCP half-close.
|
||||||
|
- SCTP message size: webrtc-rs omits `a=max-message-size`, so the viewer assumes 64 KB and refuses larger messages; the
|
||||||
|
answer sent to the viewer adds `a=max-message-size:262144` (only the sent copy — webrtc-rs rejects a modified local
|
||||||
|
description) and `vendor/webrtc` raises the data-channel receive buffer from 65535 to 262144 bytes.
|
||||||
- Cursor shape = Message 15 SystemStateChange field 2. Audio: the viewer always offers an Opus m-line.
|
- Cursor shape = Message 15 SystemStateChange field 2. Audio: the viewer always offers an Opus m-line.
|
||||||
- Login API: `/api/v1/device/windows/init` → device_id; `/qrcode/gen/login`; long-poll
|
- Login API: `/api/v1/device/windows/init` → device_id; `/qrcode/gen/login`; long-poll
|
||||||
`/qrcode/login/status` (1 wait, 2 scanned, 4 confirmed); `/login/by_qrcode` → user token. QR expires after
|
`/qrcode/login/status` (1 wait, 2 scanned, 4 confirmed); `/login/by_qrcode` → user token. QR expires after
|
||||||
~5 min with HTTP 400 code 1190 (not fatal: generate a new one).
|
~5 min with HTTP 400 code 1190 (not fatal: generate a new one).
|
||||||
|
|
||||||
|
- Remote assistance (recipient side), live-verified with the official Mac viewer; protocol from the official
|
||||||
|
Windows lite host (GameViewerLite: GameViewerServer + GameViewerLiteInner). Guest (not signed in) and account
|
||||||
|
identities use parallel endpoints:
|
||||||
|
| step | guest | signed in |
|
||||||
|
|---|---|---|
|
||||||
|
| identity | register the Windows device, then `POST /api/v1/guest/create` (422/1001 if unregistered) | account token |
|
||||||
|
| room | `POST /api/v1/guest/room/create` **before** share info (else 400/2004) | the host's own room |
|
||||||
|
| ID + code owner | `POST /api/v1/guest/share/info` → `connect_id` | `GET /api/v1/device/share/info` (POST = 405) |
|
||||||
|
| withdraw | `POST /api/v1/guest/share/cancel` | `POST /api/v1/device/share/cancel` |
|
||||||
|
Cloud pushes arrive as `bmsg_push`: `get_control_mode {control_id}` → `POST …/room/share/upload_control_mode`
|
||||||
|
`{control_id, allow_control, control_mode: by_password}` (guest v1, account v2); the viewer only asks for the code
|
||||||
|
after this. `remote_control {control_id, salt}` → `upload/sign` (guest v1) / `upload_sign` (account v2)
|
||||||
|
`{control_id, can_remote_control, sign: sha256hex(salt+code), backup_sign}`. Temporary-only: sign=temp code,
|
||||||
|
empty backup; both: backup=custom; custom-only: sign=custom. The share ID stays the same across sign-in/out.
|
||||||
|
Same-account devices cannot assist each other (viewer: 无法对同账号的设备发起远程协助). On a signed-in host never gate
|
||||||
|
the owner's offers on the assist switch; the cloud already gates assist peers.
|
||||||
|
|
||||||
## 8. Known pitfalls
|
## 8. Known pitfalls
|
||||||
|
|
||||||
- webrtc-rs `Sample.duration` sets the gap to the **next** frame. FFmpeg 6.1 rejects `-r` together with
|
- webrtc-rs `Sample.duration` sets the gap to the **next** frame. FFmpeg 6.1 rejects `-r` together with
|
||||||
@@ -187,6 +218,9 @@ started a second full host server instead, so check that a subcommand exists in
|
|||||||
- Remote keys repeat at high RTT because key-up arrives after the X autorepeat delay. XTEST repeats through the
|
- Remote keys repeat at high RTT because key-up arrives after the X autorepeat delay. XTEST repeats through the
|
||||||
master keyboard; the engine disables global autorepeat only while a remote key is held.
|
master keyboard; the engine disables global autorepeat only while a remote key is held.
|
||||||
- Interactive shells ignore SIGTERM: the server HUPs/KILLs PTY sessions on shutdown.
|
- Interactive shells ignore SIGTERM: the server HUPs/KILLs PTY sessions on shutdown.
|
||||||
|
- An explicit `DISPLAY` that cannot be opened is an error; the X11 engine falls back to `:0`/`:1` only when DISPLAY is unset.
|
||||||
|
- systemd/D-Bus-activated apps (GNOME Terminal, portals) run with the user manager's environment, not the caller's:
|
||||||
|
a private `dbus-run-session` bus does not bring them onto a virtual display.
|
||||||
- GTK windows stop repainting while GNOME is locked or the monitor is off; test UI on Xvfb or after unlock.
|
- GTK windows stop repainting while GNOME is locked or the monitor is off; test UI on Xvfb or after unlock.
|
||||||
- logind `unlock-session` by the session owner clears the GNOME shield in ~3 s.
|
- logind `unlock-session` by the session owner clears the GNOME shield in ~3 s.
|
||||||
- GNOME closes the portal session on lock; capture/input must share one portal session and be rebuilt after
|
- GNOME closes the portal session on lock; capture/input must share one portal session and be rebuilt after
|
||||||
@@ -209,3 +243,67 @@ started a second full host server instead, so check that a subcommand exists in
|
|||||||
- New protocol behavior: cite the evidence (observed field numbers, official log strings) in a comment.
|
- New protocol behavior: cite the evidence (observed field numbers, official log strings) in a comment.
|
||||||
- Update README.md / README.en.md when user-visible features or limitations change, and add
|
- Update README.md / README.en.md when user-visible features or limitations change, and add
|
||||||
`docs/release-notes/<tag>.md` before tagging a release.
|
`docs/release-notes/<tag>.md` before tagging a release.
|
||||||
|
|
||||||
|
### VAAPI (opt-in / 显式启用)
|
||||||
|
|
||||||
|
`UU_VIDEO_ENCODER=vaapi` selects Intel/AMD H.264 VAAPI; `UU_VAAPI_DEVICE` defaults to
|
||||||
|
`/dev/dri/renderD128` (render-node permissions required). No automatic VAAPI selection.
|
||||||
|
Both FFmpeg process and `--features inproc-av` upload NV12 frames to a VAAPI frame pool.
|
||||||
|
VBR uses the viewer bitrate preset and CQ-derived minimum QP, no B-frames, async depth 1,
|
||||||
|
AUD and the existing GOP/VFR timing. Unsupported device/driver/initialization falls back to libx264
|
||||||
|
with a warning. Accepted on Intel UHD 770 (iHD) with the default FFmpeg process pipeline and the official Mac
|
||||||
|
viewer (presets, fps, scaling, reconnect; no fallback, ~40% less CPU than libx264). AMD and the `inproc-av`
|
||||||
|
VAAPI path are not hardware-verified.
|
||||||
|
|
||||||
|
### Virtual audio / 虚拟声卡
|
||||||
|
|
||||||
|
The viewer's 静音运行 switch creates the null sink `uu-remote-sink`,
|
||||||
|
moves streams from the original default output and captures `uu-remote-sink.monitor`.
|
||||||
|
New output goes to this sink; streams explicitly pinned to other outputs are untouched.
|
||||||
|
The last desktop session restores the previous default output and unloads the module.
|
||||||
|
`mute_permanent` retains the physical mute policy, but still releases the virtual sink.
|
||||||
|
A private recovery record beside the config and a process lock recover crash leftovers on startup.
|
||||||
|
`pactl` is used when installed (PulseAudio / PipeWire-pulse); plain PipeWire systems without it (default
|
||||||
|
Ubuntu desktop) use `pw-cli`/`wpctl` for the same sink and WirePlumber moves playing streams. Accepted on
|
||||||
|
Ubuntu 24.04 with the official Mac viewer. When routing is unavailable, the existing mute/monitor path remains.
|
||||||
|
Changing the switch restarts only the scoped audio encoder; normal non-muted capture is unchanged.
|
||||||
|
`virtual-audio-probe` temporarily routes output for five seconds and restores it (local diagnostic).
|
||||||
|
|
||||||
|
### Virtual desktop user bus
|
||||||
|
|
||||||
|
The supervisor imports DISPLAY and XAUTHORITY into the linger user bus, waits for authenticated
|
||||||
|
X11 readiness, and keeps its cleanup trap alive. The sample drop-in uses `%t/uu-virtual-99.Xauthority`.
|
||||||
|
Use a dedicated headless account. See the [setup and official Mac acceptance checklist](contrib/virtual-desktop/ACCEPTANCE.md).
|
||||||
|
|
||||||
|
### Arch Linux / Omarchy
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sudo pacman -S --needed base-devel rust pkgconf clang ffmpeg libx11 libxext libxtst libxfixes gtk3 \
|
||||||
|
wayland libxkbcommon wl-clipboard libpipewire glib2 libpulse
|
||||||
|
cargo test --locked --features wayland
|
||||||
|
cargo build --release --features wayland
|
||||||
|
```
|
||||||
|
|
||||||
|
Omarchy uses Hyprland: run in its user session, select `UU_DESKTOP_BACKEND=wayland`,
|
||||||
|
and retain `WAYLAND_DISPLAY` / `XDG_RUNTIME_DIR`. This requires wlr-screencopy and the
|
||||||
|
virtual input protocols supported by Hyprland; other compositors may need `--features wayland,portal`.
|
||||||
|
For uinput, load `uinput` and apply the uaccess rule above; seat ACLs require an active login.
|
||||||
|
For unattended accounts, explicitly restrict `/dev/uinput` access to a dedicated group.
|
||||||
|
This permission can inject into the whole seat, including lock/greeter screens.
|
||||||
|
`UU_INPUT_BACKEND=uinput` requests kernel devices; verify startup actually reports uinput.
|
||||||
|
Optional VAAPI: `intel-media-driver` (modern Intel) or `libva-mesa-driver` (AMD), `libva-utils`
|
||||||
|
for `vainfo`. Optional KMS: `libdrm`. Virtual Xfce: `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
|
||||||
|
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`.
|
||||||
|
Rolling Arch FFmpeg may be newer than supported `inproc-av` (FFmpeg 6.x); use the default process
|
||||||
|
pipeline or build against a separate FFmpeg 6 prefix. Do not downgrade the system FFmpeg.
|
||||||
|
User-bus headless setup and the official Mac checklist: [virtual desktop acceptance](contrib/virtual-desktop/ACCEPTANCE.md).
|
||||||
|
|
||||||
|
### Experimental KMS / 实验后端
|
||||||
|
|
||||||
|
`--features kms` plus explicit `UU_DESKTOP_BACKEND=kms` enables a limited linear-framebuffer
|
||||||
|
prototype; `UU_KMS_DEVICE=/dev/dri/card0`. Requires libdrm headers and deliberately granted
|
||||||
|
`cap_sys_admin+ep`, plus card-node access. It is never automatically selected or granted privileges.
|
||||||
|
This capability is broad: it applies to the whole network-facing process and can expose greeter/lock screens that portal consent would hide. Use it only on a disposable machine with a reviewed binary; inspect with `getcap`, revoke with `sudo setcap -r <path>`. A VM greeter capture was solid black, so lock/greeter screens are not supported.
|
||||||
|
The VM GDM greeter capture was solid black (failed visual acceptance); lock/greeter support and Intel/AMD hardware remain unverified. Use uinput for input; clipboard
|
||||||
|
is unavailable without a desktop session. No default release behavior changes.
|
||||||
|
No official "no desktop" error code was found for headless desktop offers, so the existing offer behavior is retained.
|
||||||
Generated
+193
-6
@@ -111,6 +111,12 @@ dependencies = [
|
|||||||
"rustversion",
|
"rustversion",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "as-raw-xcb-connection"
|
||||||
|
version = "1.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "asn1-rs"
|
name = "asn1-rs"
|
||||||
version = "0.5.2"
|
version = "0.5.2"
|
||||||
@@ -212,6 +218,26 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bindgen"
|
||||||
|
version = "0.64.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c4243e6031260db77ede97ad86c27e501d646a27ab57b59a574f725d98ab1fb4"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 1.3.2",
|
||||||
|
"cexpr",
|
||||||
|
"clang-sys",
|
||||||
|
"lazy_static",
|
||||||
|
"lazycell",
|
||||||
|
"peeking_take_while",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"regex",
|
||||||
|
"rustc-hash 1.1.0",
|
||||||
|
"shlex 1.3.0",
|
||||||
|
"syn 1.0.109",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "1.3.2"
|
version = "1.3.2"
|
||||||
@@ -276,7 +302,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "6651c9ed80effdc7db0ff72512157f901af5e3549e341e24b1dd4887d836d838"
|
checksum = "6651c9ed80effdc7db0ff72512157f901af5e3549e341e24b1dd4887d836d838"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"find-msvc-tools",
|
"find-msvc-tools",
|
||||||
"shlex",
|
"shlex 2.0.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -291,6 +317,15 @@ dependencies = [
|
|||||||
"subtle",
|
"subtle",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cexpr"
|
||||||
|
version = "0.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
|
||||||
|
dependencies = [
|
||||||
|
"nom",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cfg-if"
|
name = "cfg-if"
|
||||||
version = "1.0.5"
|
version = "1.0.5"
|
||||||
@@ -324,6 +359,17 @@ dependencies = [
|
|||||||
"inout",
|
"inout",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clang-sys"
|
||||||
|
version = "1.9.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "157a8ba7b480713b56f4c09fd13fc3e0a22a5dfab8097ba61cbc5feef950788a"
|
||||||
|
dependencies = [
|
||||||
|
"glob",
|
||||||
|
"libc",
|
||||||
|
"libloading",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "colorchoice"
|
name = "colorchoice"
|
||||||
version = "1.0.5"
|
version = "1.0.5"
|
||||||
@@ -605,6 +651,31 @@ dependencies = [
|
|||||||
"subtle",
|
"subtle",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ffmpeg-next"
|
||||||
|
version = "6.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4e72c72e8dcf638fb0fb03f033a954691662b5dabeaa3f85a6607d101569fccd"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 1.3.2",
|
||||||
|
"ffmpeg-sys-next",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ffmpeg-sys-next"
|
||||||
|
version = "6.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c2529ad916d08c3562c754c21bc9b17a26c7882c0f5706cc2cd69472175f1620"
|
||||||
|
dependencies = [
|
||||||
|
"bindgen",
|
||||||
|
"cc",
|
||||||
|
"libc",
|
||||||
|
"num_cpus",
|
||||||
|
"pkg-config",
|
||||||
|
"vcpkg",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fiat-crypto"
|
name = "fiat-crypto"
|
||||||
version = "0.2.9"
|
version = "0.2.9"
|
||||||
@@ -736,6 +807,16 @@ dependencies = [
|
|||||||
"zeroize",
|
"zeroize",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "gethostname"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1bd49230192a3797a9a4d6abe9b3eed6f7fa4c8a8a4947977c6f80025f92cbd8"
|
||||||
|
dependencies = [
|
||||||
|
"rustix",
|
||||||
|
"windows-link",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "getrandom"
|
name = "getrandom"
|
||||||
version = "0.2.17"
|
version = "0.2.17"
|
||||||
@@ -773,6 +854,12 @@ dependencies = [
|
|||||||
"polyval",
|
"polyval",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "glob"
|
||||||
|
version = "0.3.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e4eba85ea1d0a966a983acd07deee566e67395d2d96b6fb39e62b5a833f1eb0b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "group"
|
name = "group"
|
||||||
version = "0.13.0"
|
version = "0.13.0"
|
||||||
@@ -784,6 +871,12 @@ dependencies = [
|
|||||||
"subtle",
|
"subtle",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hermit-abi"
|
||||||
|
version = "0.5.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e17592d60ebacc7d5e169f4663c5f84f9161cc90328abcfe8456f41e4dfcb284"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hex"
|
name = "hex"
|
||||||
version = "0.4.3"
|
version = "0.4.3"
|
||||||
@@ -1111,12 +1204,34 @@ version = "1.5.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb"
|
checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lazycell"
|
||||||
|
version = "1.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.190"
|
version = "0.2.190"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ce5d3ddc6d3fa000eb1536d85e147bfe31aacaba692ed6a876f95cb7c855be78"
|
checksum = "ce5d3ddc6d3fa000eb1536d85e147bfe31aacaba692ed6a876f95cb7c855be78"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libloading"
|
||||||
|
version = "0.8.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"windows-link",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "linux-raw-sys"
|
||||||
|
version = "0.12.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "litemap"
|
name = "litemap"
|
||||||
version = "0.8.3"
|
version = "0.8.3"
|
||||||
@@ -1265,6 +1380,16 @@ dependencies = [
|
|||||||
"autocfg",
|
"autocfg",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num_cpus"
|
||||||
|
version = "1.17.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b"
|
||||||
|
dependencies = [
|
||||||
|
"hermit-abi",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "oid-registry"
|
name = "oid-registry"
|
||||||
version = "0.6.1"
|
version = "0.6.1"
|
||||||
@@ -1339,6 +1464,12 @@ dependencies = [
|
|||||||
"windows-link",
|
"windows-link",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "peeking_take_while"
|
||||||
|
version = "0.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pem"
|
name = "pem"
|
||||||
version = "3.0.6"
|
version = "3.0.6"
|
||||||
@@ -1386,6 +1517,12 @@ dependencies = [
|
|||||||
"spki",
|
"spki",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pkg-config"
|
||||||
|
version = "0.3.34"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "polyval"
|
name = "polyval"
|
||||||
version = "0.6.2"
|
version = "0.6.2"
|
||||||
@@ -1472,7 +1609,7 @@ dependencies = [
|
|||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"quinn-proto",
|
"quinn-proto",
|
||||||
"quinn-udp",
|
"quinn-udp",
|
||||||
"rustc-hash",
|
"rustc-hash 2.1.3",
|
||||||
"rustls 0.23.45",
|
"rustls 0.23.45",
|
||||||
"socket2 0.6.5",
|
"socket2 0.6.5",
|
||||||
"thiserror 2.0.21",
|
"thiserror 2.0.21",
|
||||||
@@ -1493,7 +1630,7 @@ dependencies = [
|
|||||||
"rand 0.10.3",
|
"rand 0.10.3",
|
||||||
"rand_pcg",
|
"rand_pcg",
|
||||||
"ring 0.17.14",
|
"ring 0.17.14",
|
||||||
"rustc-hash",
|
"rustc-hash 2.1.3",
|
||||||
"rustls 0.23.45",
|
"rustls 0.23.45",
|
||||||
"rustls-pki-types",
|
"rustls-pki-types",
|
||||||
"slab",
|
"slab",
|
||||||
@@ -1740,6 +1877,12 @@ dependencies = [
|
|||||||
"webrtc-util",
|
"webrtc-util",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustc-hash"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustc-hash"
|
name = "rustc-hash"
|
||||||
version = "2.1.3"
|
version = "2.1.3"
|
||||||
@@ -1764,6 +1907,19 @@ dependencies = [
|
|||||||
"nom",
|
"nom",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustix"
|
||||||
|
version = "1.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "891efababe418670775f199f0d233d84843c227a0949a883ce15b37c78d6629d"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.13.2",
|
||||||
|
"errno",
|
||||||
|
"libc",
|
||||||
|
"linux-raw-sys",
|
||||||
|
"windows-sys 0.61.2",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustls"
|
name = "rustls"
|
||||||
version = "0.21.12"
|
version = "0.21.12"
|
||||||
@@ -1958,6 +2114,12 @@ dependencies = [
|
|||||||
"digest",
|
"digest",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "shlex"
|
||||||
|
version = "1.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "shlex"
|
name = "shlex"
|
||||||
version = "2.0.1"
|
version = "2.0.1"
|
||||||
@@ -2487,12 +2649,13 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "uu-remote-linux"
|
name = "uu-remote-linux"
|
||||||
version = "0.1.2"
|
version = "0.1.13"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64 0.22.1",
|
"base64 0.22.1",
|
||||||
"bytes",
|
"bytes",
|
||||||
"cc",
|
"cc",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
|
"ffmpeg-next",
|
||||||
"flate2",
|
"flate2",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"hex",
|
"hex",
|
||||||
@@ -2508,6 +2671,7 @@ dependencies = [
|
|||||||
"tokio",
|
"tokio",
|
||||||
"tokio-tungstenite",
|
"tokio-tungstenite",
|
||||||
"webrtc",
|
"webrtc",
|
||||||
|
"x11rb",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2521,6 +2685,12 @@ dependencies = [
|
|||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "vcpkg"
|
||||||
|
version = "0.2.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "version_check"
|
name = "version_check"
|
||||||
version = "0.9.5"
|
version = "0.9.5"
|
||||||
@@ -2648,8 +2818,6 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "webrtc"
|
name = "webrtc"
|
||||||
version = "0.10.1"
|
version = "0.10.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "1fbdf025f0fa62f4bf252b2fb0cff0a04d3eac2021c440096649e62f4e48553d"
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"arc-swap",
|
"arc-swap",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
@@ -2962,6 +3130,25 @@ version = "0.6.4"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc"
|
checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "x11rb"
|
||||||
|
version = "0.13.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9993aa5be5a26815fe2c3eacfc1fde061fc1a1f094bf1ad2a18bf9c495dd7414"
|
||||||
|
dependencies = [
|
||||||
|
"as-raw-xcb-connection",
|
||||||
|
"gethostname",
|
||||||
|
"libc",
|
||||||
|
"rustix",
|
||||||
|
"x11rb-protocol",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "x11rb-protocol"
|
||||||
|
version = "0.13.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ea6fc2961e4ef194dcbfe56bb845534d0dc8098940c7e5c012a258bfec6701bd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "x25519-dalek"
|
name = "x25519-dalek"
|
||||||
version = "2.0.1"
|
version = "2.0.1"
|
||||||
|
|||||||
+10
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "uu-remote-linux"
|
name = "uu-remote-linux"
|
||||||
version = "0.1.2"
|
version = "0.1.13"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["uu-remote-linux contributors"]
|
authors = ["uu-remote-linux contributors"]
|
||||||
description = "Unofficial Linux host for NetEase UU Remote: control a Linux desktop from the official clients"
|
description = "Unofficial Linux host for NetEase UU Remote: control a Linux desktop from the official clients"
|
||||||
@@ -16,7 +16,12 @@ path = "src/main.rs"
|
|||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
wayland = []
|
wayland = []
|
||||||
|
# Experimental direct linear framebuffer capture; never enabled in release builds.
|
||||||
|
kms = []
|
||||||
portal = []
|
portal = []
|
||||||
|
# In-process capture and encoding through FFmpeg 6 libraries (libavcodec/libavdevice dev packages).
|
||||||
|
# Off by default so release binaries do not link the build machine's libav.
|
||||||
|
inproc-av = ["dep:ffmpeg-next", "dep:x11rb"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tokio = { version = "1", features = ["full"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
@@ -37,9 +42,13 @@ nix = { version = "0.29", features = ["term", "process", "fs", "ioctl", "signal"
|
|||||||
log = "0.4"
|
log = "0.4"
|
||||||
qrcode = { version = "0.14", default-features = false }
|
qrcode = { version = "0.14", default-features = false }
|
||||||
env_logger = "0.11"
|
env_logger = "0.11"
|
||||||
|
ffmpeg-next = { version = "6.1", optional = true, default-features = false, features = ["codec", "format", "device", "software-scaling", "software-resampling"] }
|
||||||
|
x11rb = { version = "0.13", features = ["damage", "shm", "allow-unsafe-code"], optional = true }
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
cc = "1.0"
|
cc = "1.0"
|
||||||
|
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
webrtc-dtls = { path = "vendor/webrtc-dtls" }
|
webrtc-dtls = { path = "vendor/webrtc-dtls" }
|
||||||
|
# Larger data-channel receive buffer (see vendor/webrtc/src/data_channel/mod.rs).
|
||||||
|
webrtc = { path = "vendor/webrtc" }
|
||||||
+168
-9
@@ -12,7 +12,7 @@
|
|||||||
> If NetEase considers this project infringing or inappropriate, please DM me on X
|
> If NetEase considers this project infringing or inappropriate, please DM me on X
|
||||||
> **[@ai_fengshou](https://x.com/ai_fengshou)** and I will take it down right away.
|
> **[@ai_fengshou](https://x.com/ai_fengshou)** and I will take it down right away.
|
||||||
|
|
||||||
<p align="center"><img src="docs/images/login-window.png" width="560" alt="QR login window on the Linux host"></p>
|
<p align="center"><img src="docs/images/main-window.png" width="620" alt="Host main window: remote assistance and account"></p>
|
||||||
<p align="center"><sub>The QR login window shown on first run (QR code blurred). The UI is in Chinese, like the official app.</sub></p>
|
<p align="center"><sub>The QR login window shown on first run (QR code blurred). The UI is in Chinese, like the official app.</sub></p>
|
||||||
|
|
||||||
<!-- Screenshot placeholder: the Linux desktop controlled from the official client. Take your own
|
<!-- Screenshot placeholder: the Linux desktop controlled from the official client. Take your own
|
||||||
@@ -31,9 +31,11 @@
|
|||||||
|
|
||||||
| Feature | Notes |
|
| Feature | Notes |
|
||||||
|---|---|
|
|---|---|
|
||||||
| QR login | GTK login window (or a terminal QR code with `login --no-gui`); expired codes refresh automatically; the service opens the login window by itself when credentials are missing or expired |
|
| Main window and QR login | One window with "This device" (remote assistance) and "Account": a QR code before sign-in, the account and a sign-out button after; `login --no-gui` prints the QR in a terminal |
|
||||||
|
| Remote assistance (experimental) | Like the official client: share the **device ID + verification code**; the other person enters them under 开始协助 in UU Remote. Works before sign-in (guest identity) and after; see below |
|
||||||
| Remote desktop | H.264 video, NVENC hardware encoding on NVIDIA GPUs, libx264 otherwise; the viewer draws its local pointer, so there is no double cursor |
|
| Remote desktop | H.264 video, NVENC hardware encoding on NVIDIA GPUs, libx264 otherwise; the viewer draws its local pointer, so there is no double cursor |
|
||||||
| Mouse / keyboard | Absolute and relative motion, clicks, wheel, keys; no spurious key repeat on high-latency links |
|
| Mouse / keyboard | Absolute and relative motion, clicks, wheel, keys; no spurious key repeat on high-latency links; optional uinput virtual devices on Wayland (layout independent, see below) |
|
||||||
|
| Mac shortcuts | **Recommended**: turn off 「使用 macOS 快捷键」 (use macOS shortcuts) in the Mac viewer's remote-window menu; then Cmd+C/V/A/F/X/Z work as on a Mac (Cmd+C/V become Ctrl+Shift+C/V in terminals) and Ctrl+C always interrupts. With the option on, the viewer sends Cmd as Ctrl: in a terminal Ctrl+V / Cmd+V paste, Ctrl+C interrupts, copy is Cmd+Shift+C (X11) |
|
||||||
| Pointer shape | Remote cursor shape is mirrored to the viewer (X11) |
|
| Pointer shape | Remote cursor shape is mirrored to the viewer (X11) |
|
||||||
| Clipboard | Two-way text sync |
|
| Clipboard | Two-way text sync |
|
||||||
| Quality / frame rate / resolution | Auto, Original 20M, Ultra 8M, HD 2M, custom; 30/60/90/144 fps; "other resolutions" are scaled by the encoder, the host monitor is left untouched |
|
| Quality / frame rate / resolution | Auto, Original 20M, Ultra 8M, HD 2M, custom; 30/60/90/144 fps; "other resolutions" are scaled by the encoder, the host monitor is left untouched |
|
||||||
@@ -44,7 +46,15 @@
|
|||||||
| Lock after remote session | Stored on the host, so it still applies after reconnecting |
|
| Lock after remote session | Stored on the host, so it still applies after reconnecting |
|
||||||
| Mute host while controlled | Host speakers are muted while the viewer still hears the sound; the previous mute state is restored afterwards |
|
| Mute host while controlled | Host speakers are muted while the viewer still hears the sound; the previous mute state is restored afterwards |
|
||||||
| System actions | Lock, show desktop, task manager (system monitor), reboot, shut down |
|
| System actions | Lock, show desktop, task manager (system monitor), reboot, shut down |
|
||||||
|
| Port mapping | The official "port mapping" tool: forwards a local port on the viewer to an address reachable from the Linux host (e.g. 127.0.0.1:22); `UU_PORT_MAPPING=0` disables it |
|
||||||
| Reconnects | Every tool window has its own connection; they do not disturb each other |
|
| Reconnects | Every tool window has its own connection; they do not disturb each other |
|
||||||
|
| Headless servers | Goes online without a monitor with terminal, file transfer and port mapping; an Xvfb virtual desktop gives a full remote desktop (see below) |
|
||||||
|
|
||||||
|
Desktop connection boundaries and host startup clear stale input; delayed events
|
||||||
|
from an old desktop cannot affect its replacement. X11 clears pressed state on
|
||||||
|
the authenticated display (including physical keys whose origin it cannot
|
||||||
|
distinguish); uinput clears only this host's virtual devices, and Portal only its
|
||||||
|
authorized session. Independent file/terminal tool peers do not reset input.
|
||||||
|
|
||||||
## Supported Linux desktops
|
## Supported Linux desktops
|
||||||
|
|
||||||
@@ -61,10 +71,18 @@ Apart from GNOME on X11, these desktops have been tested in virtual machines so
|
|||||||
real hardware, please [open an issue](../../issues) with your distribution, desktop, GPU and the relevant lines from
|
real hardware, please [open an issue](../../issues) with your distribution, desktop, GPU and the relevant lines from
|
||||||
`journalctl --user -u uuremote-native.service`.
|
`journalctl --user -u uuremote-native.service`.
|
||||||
|
|
||||||
Wayland limits: single screen, fixed resolution. The portal backend does not support clipboard, screen wake or
|
Wayland limits: single screen, fixed resolution. The native Hyprland backend now pipelines two shm screencopy buffers and passes the viewer's requested fps to both capture pacing and the encoder. Actual fps remains limited by the compositor, rendering and readback (the old serial path measured about 16–22 fps at 2560x1600; the pipeline is VM-measured only, not a hardware guarantee). The portal backend does not support screen wake or
|
||||||
dynamic resolution yet. On the first connection the desktop asks for "Remote Desktop / Screen Sharing"
|
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.
|
permission; the permission is remembered once granted.
|
||||||
|
|
||||||
|
Native Wayland experiments: `UU_WAYLAND_PIPELINE=0` disables pipelining for comparison.
|
||||||
|
`UU_WAYLAND_DMABUF=1` explicitly tries dma-buf → GBM CPU mapping (off by default;
|
||||||
|
requires runtime libgbm; `UU_WAYLAND_DMABUF_DEVICE` overrides `/dev/dri/renderD128`).
|
||||||
|
Only single-plane 8-bit RGB with an advertised linear modifier is supported;
|
||||||
|
failure falls back to SHM. The virgl VM verified fallback only: successful import,
|
||||||
|
mapping and hardware acceleration remain unverified. This is not zero-copy VAAPI;
|
||||||
|
`UU_VIDEO_ENCODER=vaapi` still uploads CPU frames through FFmpeg.
|
||||||
|
|
||||||
## Not implemented / not supported
|
## Not implemented / not supported
|
||||||
|
|
||||||
| Feature | Status |
|
| Feature | Status |
|
||||||
@@ -76,8 +94,7 @@ permission; the permission is remembered once granted.
|
|||||||
| Privacy screen on the host | Not implemented |
|
| Privacy screen on the host | Not implemented |
|
||||||
| Microphone (viewer audio played on the host) | Not implemented (needs a virtual sound card) |
|
| Microphone (viewer audio played on the host) | Not implemented (needs a virtual sound card) |
|
||||||
| Plugins such as the WoW queue notifier | Not supported; the client is told that no plugins exist |
|
| Plugins such as the WoW queue notifier | Not supported; the client is told that no plugins exist |
|
||||||
| Access before login (greeter / no user logged in) | Not supported; only an already logged-in desktop session can be controlled |
|
| Linux login screen | The system login screen (before anyone logs in to the Linux desktop) cannot be controlled yet; use headless mode or a virtual desktop (see below). This is about the Linux login: **without a UU account** the host can now be used through remote assistance |
|
||||||
| Port mapping | Code exists, not yet accepted |
|
|
||||||
| Relay connections on restrictive networks | Uses the ICE servers provided by the UU cloud; complex network setups are not thoroughly tested |
|
| Relay connections on restrictive networks | Uses the ICE servers provided by the UU cloud; complex network setups are not thoroughly tested |
|
||||||
| Acting as a viewer (controlling other machines from Linux) | Out of scope |
|
| Acting as a viewer (controlling other machines from Linux) | Out of scope |
|
||||||
|
|
||||||
@@ -149,6 +166,79 @@ journalctl --user -u uuremote-native.service -f # online once you see availabi
|
|||||||
|
|
||||||
If you have not logged in yet, the service opens the QR login window on your desktop.
|
If you have not logged in yet, the service opens the QR login window on your desktop.
|
||||||
|
|
||||||
|
### 5. uinput virtual keyboard/mouse on Wayland (recommended)
|
||||||
|
|
||||||
|
On Wayland (especially GNOME/KDE via the portal) the default input path needs portal consent and some keyboard layouts
|
||||||
|
drop symbols such as the underscore. Once the logged-in user may access `/dev/uinput`, the host switches to kernel
|
||||||
|
virtual devices that send physical key codes:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
echo 'KERNEL=="uinput", SUBSYSTEM=="misc", OPTIONS+="static_node=uinput", TAG+="uaccess"' | \
|
||||||
|
sudo tee /etc/udev/rules.d/60-uu-remote-linux-uinput.rules
|
||||||
|
sudo udevadm control --reload && sudo udevadm trigger --name-match=uinput
|
||||||
|
```
|
||||||
|
|
||||||
|
`uaccess` grants access only to the user sitting at the machine. `UU_INPUT_BACKEND=uinput|native` forces a backend.
|
||||||
|
X11 keeps XTest by default because it handles held keys correctly on high-latency links.
|
||||||
|
|
||||||
|
## Remote assistance and the main window (experimental)
|
||||||
|
|
||||||
|
As in the official client, this host shows a **device ID** and a **verification code**; whoever you give them to can
|
||||||
|
connect from 开始协助 (remote assistance) in UU Remote.
|
||||||
|
|
||||||
|
- **Not signed in**: the main window opens at start, offers remote assistance as a guest and shows the sign-in QR code.
|
||||||
|
Signing in switches to account mode automatically.
|
||||||
|
- **Signed in**: the host is in your device list and remote assistance keeps working (same device ID). A background
|
||||||
|
service keeps the window hidden; open it with `uuremote-server show` or the "UU Remote Linux Host" launcher.
|
||||||
|
Closing the window only hides it.
|
||||||
|
- **Tray icon**: a background host shows a tray icon with Open main window, the allow switch and Quit. GNOME needs
|
||||||
|
the AppIndicator extension (bundled on Ubuntu); Xfce / Cinnamon / KDE work as is. Quit stops `uuremote-native.service`;
|
||||||
|
the launcher starts it again.
|
||||||
|
- **Settings** (as in the official client): allow switch (off by default); verification by temporary code, custom code
|
||||||
|
(8–16 letters and digits) or either; temporary code rotation after each session / hourly / 12 h / 24 h / never;
|
||||||
|
copy buttons for the ID and the code. Stored in the 0600 config.
|
||||||
|
- The code is only shown in the local window, never logged. Without a desktop, `uuremote-server assist` shows it on the
|
||||||
|
terminal (`allow` / `deny` / `reset` / `mode …` / `refresh …` / `off`).
|
||||||
|
- **Security**: the ID and code give full control (desktop, files, terminal). Share them only with people you trust.
|
||||||
|
|
||||||
|
Optional launcher icon:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
install -Dm644 contrib/desktop/uu-remote-linux.svg ~/.local/share/icons/hicolor/scalable/apps/uu-remote-linux.svg
|
||||||
|
install -Dm644 contrib/desktop/uu-remote-linux.desktop ~/.local/share/applications/uu-remote-linux.desktop
|
||||||
|
```
|
||||||
|
|
||||||
|
Verified with the official Mac viewer: guest connections, wrong-code rejection, code rotation, switching assistance off,
|
||||||
|
signing in and out. **Remote assistance while signed in has not yet been tested from a different account** (devices of
|
||||||
|
the same account cannot assist each other, an official restriction).
|
||||||
|
|
||||||
|
## Servers without a monitor
|
||||||
|
|
||||||
|
**Headless mode** (terminal, file transfer and port mapping only): add `Environment=UU_DESKTOP_BACKEND=none` to the
|
||||||
|
service and run `loginctl enable-linger $USER`, so the systemd user manager (with `XDG_RUNTIME_DIR` and the user D-Bus)
|
||||||
|
starts at boot without a login. The viewer's remote desktop keeps showing "connecting" in this mode; that is expected.
|
||||||
|
Log in once with `uuremote-server login --no-gui` (QR in the terminal).
|
||||||
|
|
||||||
|
**Virtual desktop**: for a full remote desktop, run a desktop session (Xfce recommended) on an Xvfb screen:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sudo apt install xvfb xauth x11-utils dbus-x11 dbus-user-session xfce4 # or another session, chosen with UU_VIRTUAL_SESSION
|
||||||
|
install -Dm755 contrib/virtual-desktop/uu-virtual-desktop ~/.local/bin/uu-virtual-desktop
|
||||||
|
install -Dm644 contrib/virtual-desktop/uu-virtual-desktop.service ~/.config/systemd/user/uu-virtual-desktop.service
|
||||||
|
install -Dm644 contrib/virtual-desktop/virtual-display.conf ~/.config/systemd/user/uuremote-native.service.d/virtual-display.conf
|
||||||
|
loginctl enable-linger $USER
|
||||||
|
systemctl --user daemon-reload && systemctl --user enable --now uu-virtual-desktop.service
|
||||||
|
systemctl --user restart uuremote-native.service
|
||||||
|
```
|
||||||
|
|
||||||
|
The virtual screen is `:99`, 1920x1080 by default. Xvfb has no GPU acceleration, so it is a little less smooth than a
|
||||||
|
real display; NVENC is still used for encoding when an NVIDIA GPU is present. The script exports `DISPLAY` to the
|
||||||
|
systemd user session so D-Bus-activated programs open on the virtual screen; on a machine that also runs a real desktop
|
||||||
|
session set `UU_VIRTUAL_BUS=private` so the real session is not affected.
|
||||||
|
|
||||||
|
To make a GPU drive a real (non-Xvfb) desktop without a monitor, use a dummy HDMI plug or load an EDID file with the
|
||||||
|
kernel parameter `drm.edid_firmware=`, then install as for a normal X11 desktop.
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
Common environment variables:
|
Common environment variables:
|
||||||
@@ -156,11 +246,16 @@ Common environment variables:
|
|||||||
| Variable | Purpose |
|
| Variable | Purpose |
|
||||||
|---|---|
|
|---|---|
|
||||||
| `UU_CONFIG` | Config file path (default `~/.config/uuremote/config.json`) |
|
| `UU_CONFIG` | Config file path (default `~/.config/uuremote/config.json`) |
|
||||||
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` |
|
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` / `kms` (experimental feature) / `none` (headless) |
|
||||||
| `UU_VIDEO_ENCODER=cpu` | Force CPU encoding |
|
| `UU_INPUT_BACKEND` | `auto` (default: uinput on Wayland when accessible) / `uinput` / `native` |
|
||||||
|
| `UU_TERMINAL_SHORTCUTS=0` | Disable the Ctrl+V paste rewrite in terminal windows |
|
||||||
|
| `UU_VIDEO_ENCODER` | `cpu`: libx264; `vaapi`: opt-in Intel/AMD; `nvenc`: force NVENC; unset: NVENC only when an NVIDIA driver is loaded, else CPU |
|
||||||
| `UU_FFMPEG_ROOT` | Use FFmpeg from this prefix |
|
| `UU_FFMPEG_ROOT` | Use FFmpeg from this prefix |
|
||||||
| `UU_AUDIO=0` | Disable sound; `UU_AUDIO_SOURCE` picks the PulseAudio source |
|
| `UU_AUDIO=0` | Disable sound; `UU_AUDIO_SOURCE` picks the PulseAudio source |
|
||||||
|
| `UU_REMOTE_ASSIST=0` | Disable remote assistance (no guest assistance before sign-in either) |
|
||||||
|
| `UU_ASSIST_ALLOW=1` | Allow remote assistance at start (overrides the saved switch) |
|
||||||
| `UU_LOGIN_WINDOW=0` | Do not open the login window automatically when credentials are missing |
|
| `UU_LOGIN_WINDOW=0` | Do not open the login window automatically when credentials are missing |
|
||||||
|
| `UU_PORT_MAPPING=0` | Disable port mapping (the official host's controlled-side switch) |
|
||||||
|
|
||||||
Host settings such as "lock after remote session" and "mute host" are stored in
|
Host settings such as "lock after remote session" and "mute host" are stored in
|
||||||
`~/.config/uuremote/host_settings.json`.
|
`~/.config/uuremote/host_settings.json`.
|
||||||
@@ -215,3 +310,67 @@ Released under the [GPL-3.0](LICENSE): you may use, modify and redistribute it,
|
|||||||
- The NetEase UU Remote team, for a remote tool that is a pleasure to use.
|
- The NetEase UU Remote team, for a remote tool that is a pleasure to use.
|
||||||
- [webrtc-rs](https://github.com/webrtc-rs/webrtc), FFmpeg, GTK and the other open-source projects this builds on.
|
- [webrtc-rs](https://github.com/webrtc-rs/webrtc), FFmpeg, GTK and the other open-source projects this builds on.
|
||||||
- Most of the code was written with the help of Claude and Codex.
|
- Most of the code was written with the help of Claude and Codex.
|
||||||
|
|
||||||
|
### VAAPI (opt-in / 显式启用)
|
||||||
|
|
||||||
|
`UU_VIDEO_ENCODER=vaapi` selects Intel/AMD H.264 VAAPI; `UU_VAAPI_DEVICE` defaults to
|
||||||
|
`/dev/dri/renderD128` (render-node permissions required). No automatic VAAPI selection.
|
||||||
|
Both FFmpeg process and `--features inproc-av` upload NV12 frames to a VAAPI frame pool.
|
||||||
|
VBR uses the viewer bitrate preset and CQ-derived minimum QP, no B-frames, async depth 1,
|
||||||
|
AUD and the existing GOP/VFR timing. Unsupported device/driver/initialization falls back to libx264
|
||||||
|
with a warning. Accepted on Intel UHD 770 (iHD) with the default FFmpeg process pipeline and the official Mac
|
||||||
|
viewer (presets, fps, scaling, reconnect; no fallback, ~40% less CPU than libx264). AMD and the `inproc-av`
|
||||||
|
VAAPI path are not hardware-verified.
|
||||||
|
|
||||||
|
### Virtual audio / 虚拟声卡
|
||||||
|
|
||||||
|
The viewer's 静音运行 switch creates the null sink `uu-remote-sink`,
|
||||||
|
moves streams from the original default output and captures `uu-remote-sink.monitor`.
|
||||||
|
New output goes to this sink; streams explicitly pinned to other outputs are untouched.
|
||||||
|
The last desktop session restores the previous default output and unloads the module.
|
||||||
|
`mute_permanent` retains the physical mute policy, but still releases the virtual sink.
|
||||||
|
A private recovery record beside the config and a process lock recover crash leftovers on startup.
|
||||||
|
`pactl` is used when installed (PulseAudio / PipeWire-pulse); plain PipeWire systems without it (default
|
||||||
|
Ubuntu desktop) use `pw-cli`/`wpctl` for the same sink and WirePlumber moves playing streams. Accepted on
|
||||||
|
Ubuntu 24.04 with the official Mac viewer. When routing is unavailable, the existing mute/monitor path remains.
|
||||||
|
Changing the switch restarts only the scoped audio encoder; normal non-muted capture is unchanged.
|
||||||
|
`virtual-audio-probe` temporarily routes output for five seconds and restores it (local diagnostic).
|
||||||
|
|
||||||
|
### Virtual desktop user bus
|
||||||
|
|
||||||
|
The supervisor imports DISPLAY and XAUTHORITY into the linger user bus, waits for authenticated
|
||||||
|
X11 readiness, and keeps its cleanup trap alive. The sample drop-in uses `%t/uu-virtual-99.Xauthority`.
|
||||||
|
Use a dedicated headless account. See the [setup and official Mac acceptance checklist](contrib/virtual-desktop/ACCEPTANCE.md).
|
||||||
|
|
||||||
|
### Arch Linux / Omarchy
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sudo pacman -S --needed base-devel rust pkgconf clang ffmpeg libx11 libxext libxtst libxfixes gtk3 \
|
||||||
|
wayland libxkbcommon wl-clipboard libpipewire glib2 libpulse
|
||||||
|
cargo test --locked --features wayland
|
||||||
|
cargo build --release --features wayland
|
||||||
|
```
|
||||||
|
|
||||||
|
Omarchy uses Hyprland: run in its user session, select `UU_DESKTOP_BACKEND=wayland`,
|
||||||
|
and retain `WAYLAND_DISPLAY` / `XDG_RUNTIME_DIR`. This requires wlr-screencopy and the
|
||||||
|
virtual input protocols supported by Hyprland; other compositors may need `--features wayland,portal`.
|
||||||
|
For uinput, load `uinput` and apply the uaccess rule above; seat ACLs require an active login.
|
||||||
|
For unattended accounts, explicitly restrict `/dev/uinput` access to a dedicated group.
|
||||||
|
This permission can inject into the whole seat, including lock/greeter screens.
|
||||||
|
`UU_INPUT_BACKEND=uinput` requests kernel devices; verify startup actually reports uinput.
|
||||||
|
Optional VAAPI: `intel-media-driver` (modern Intel) or `libva-mesa-driver` (AMD), `libva-utils`
|
||||||
|
for `vainfo`. Optional KMS: `libdrm`. Virtual Xfce: `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
|
||||||
|
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`.
|
||||||
|
Rolling Arch FFmpeg may be newer than supported `inproc-av` (FFmpeg 6.x); use the default process
|
||||||
|
pipeline or build against a separate FFmpeg 6 prefix. Do not downgrade the system FFmpeg.
|
||||||
|
User-bus headless setup and the official Mac checklist: [virtual desktop acceptance](contrib/virtual-desktop/ACCEPTANCE.md).
|
||||||
|
|
||||||
|
### Experimental KMS / 实验后端
|
||||||
|
|
||||||
|
`--features kms` plus explicit `UU_DESKTOP_BACKEND=kms` enables a limited linear-framebuffer
|
||||||
|
prototype; `UU_KMS_DEVICE=/dev/dri/card0`. Requires libdrm headers and deliberately granted
|
||||||
|
`cap_sys_admin+ep`, plus card-node access. It is never automatically selected or granted privileges.
|
||||||
|
This capability is broad: it applies to the whole network-facing process and can expose greeter/lock screens that portal consent would hide. Use it only on a disposable machine with a reviewed binary; inspect with `getcap`, revoke with `sudo setcap -r <path>`. A VM greeter capture was solid black, so lock/greeter screens are not supported.
|
||||||
|
The VM GDM greeter capture was solid black (failed visual acceptance); lock/greeter support and Intel/AMD hardware remain unverified. Use uinput for input; clipboard
|
||||||
|
is unavailable without a desktop session. No default release behavior changes.
|
||||||
|
No official "no desktop" error code was found for headless desktop offers, so the existing offer behavior is retained.
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
> 这是个人兴趣项目,**与网易无关,也不是官方客户端**。如果网易认为本项目侵权或有不妥,
|
> 这是个人兴趣项目,**与网易无关,也不是官方客户端**。如果网易认为本项目侵权或有不妥,
|
||||||
> 请在推特私信我 **[@ai_fengshou](https://x.com/ai_fengshou)**,我会第一时间自行下架。
|
> 请在推特私信我 **[@ai_fengshou](https://x.com/ai_fengshou)**,我会第一时间自行下架。
|
||||||
|
|
||||||
<p align="center"><img src="docs/images/login-window.png" width="560" alt="Linux 主机扫码登录窗口"></p>
|
<p align="center"><img src="docs/images/main-window.png" width="620" alt="Linux 主机主窗口:远程协助与账号"></p>
|
||||||
<p align="center"><sub>首次运行时弹出的扫码登录窗口(二维码已打码)</sub></p>
|
<p align="center"><sub>首次运行时弹出的扫码登录窗口(二维码已打码)</sub></p>
|
||||||
|
|
||||||
<!-- 截图占位:在官方客户端里远控 Linux 桌面的效果图。请自行截图并去掉账号、设备名等信息后放到
|
<!-- 截图占位:在官方客户端里远控 Linux 桌面的效果图。请自行截图并去掉账号、设备名等信息后放到
|
||||||
@@ -27,9 +27,11 @@
|
|||||||
|
|
||||||
| 功能 | 说明 |
|
| 功能 | 说明 |
|
||||||
|---|---|
|
|---|---|
|
||||||
| 扫码登录 | GTK 登录窗口(或 `login --no-gui` 终端二维码);二维码过期自动刷新;登录凭证失效时服务会自动弹出登录窗口 |
|
| 主窗口与扫码登录 | 一个窗口包含「本设备」(远程协助)和「账号」两部分:未登录时显示扫码二维码,登录后显示账号并可退出登录;也可用 `login --no-gui` 在终端扫码 |
|
||||||
|
| 远程协助(实验性) | 像官方客户端一样,把**设备 ID + 验证码**发给别人,对方在 UU「开始协助」里输入即可连接;登录前(访客身份)和登录后都能用,见下文 |
|
||||||
| 远程桌面 | H.264 视频,NVIDIA 显卡用 NVENC 硬件编码,否则用 libx264;查看端绘制本地鼠标,不会出现双光标 |
|
| 远程桌面 | H.264 视频,NVIDIA 显卡用 NVENC 硬件编码,否则用 libx264;查看端绘制本地鼠标,不会出现双光标 |
|
||||||
| 鼠标 / 键盘 | 绝对/相对移动、点击、滚轮、按键;修复了网络延迟导致的按键连发 |
|
| 鼠标 / 键盘 | 绝对/相对移动、点击、滚轮、按键;修复了网络延迟导致的按键连发;Wayland 下可用 uinput 虚拟键鼠(不依赖键盘布局,见下文) |
|
||||||
|
| Mac 快捷键 | **推荐**:在 Mac 远控窗口菜单里关闭「使用 macOS 快捷键」,之后和在 Mac 上一样:Cmd+C/V/A/F/X/Z 是复制、粘贴、全选、查找、剪切、撤销(终端里 Cmd+C/V 自动变成 Ctrl+Shift+C/V),Ctrl+C 在终端里始终是中断。保持开启时,客户端会把 Cmd 和 Ctrl 都当成 Ctrl 发送:终端里 Ctrl+V / Cmd+V 自动转成粘贴,Ctrl+C 是中断,复制用 Cmd+Shift+C(X11) |
|
||||||
| 鼠标指针样式 | 远端指针形状实时同步(X11) |
|
| 鼠标指针样式 | 远端指针形状实时同步(X11) |
|
||||||
| 剪贴板 | 双向文本同步 |
|
| 剪贴板 | 双向文本同步 |
|
||||||
| 画质 / 帧率 / 分辨率 | 自动、原画 20M、超清 8M、高清 2M、自定义;30/60/90/144 帧;「其他分辨率」由编码器缩放,不改动主机显示器 |
|
| 画质 / 帧率 / 分辨率 | 自动、原画 20M、超清 8M、高清 2M、自定义;30/60/90/144 帧;「其他分辨率」由编码器缩放,不改动主机显示器 |
|
||||||
@@ -40,7 +42,13 @@
|
|||||||
| 远程结束后自动锁屏 | 设置保存在本机,重连后依然生效 |
|
| 远程结束后自动锁屏 | 设置保存在本机,重连后依然生效 |
|
||||||
| 被控端静音运行 | 主机扬声器静音,控制端照常能听到声音;结束后恢复原来的音量设置 |
|
| 被控端静音运行 | 主机扬声器静音,控制端照常能听到声音;结束后恢复原来的音量设置 |
|
||||||
| 系统操作 | 锁屏、显示桌面、任务管理器(系统监视器)、重启、关机 |
|
| 系统操作 | 锁屏、显示桌面、任务管理器(系统监视器)、重启、关机 |
|
||||||
|
| 端口映射 | 官方「端口映射」工具:把控制端本地端口转发到 Linux 能访问到的地址(如 127.0.0.1:22);`UU_PORT_MAPPING=0` 可关闭 |
|
||||||
| 多次重连 | 每个工具窗口独立连接,互不影响 |
|
| 多次重连 | 每个工具窗口独立连接,互不影响 |
|
||||||
|
| 无头服务器 | 没有显示器也能上线,提供终端、文件传输和端口映射;也可以用 Xvfb 虚拟桌面提供完整远程桌面(见下文) |
|
||||||
|
|
||||||
|
桌面连接建立、结束及服务启动时会清理遗留输入状态;旧连接的迟到事件不能影响新的桌面连接。
|
||||||
|
X11 清理当前已认证显示上的按下状态(包括无法区分来源的实际按键);uinput 仅清理本服务虚拟设备,
|
||||||
|
Portal 仅作用于本服务获授权的会话。文件传输、终端等独立工具连接不触发桌面松键。
|
||||||
|
|
||||||
## 支持的 Linux 桌面
|
## 支持的 Linux 桌面
|
||||||
|
|
||||||
@@ -56,7 +64,9 @@
|
|||||||
除 GNOME(X11)外,其余桌面目前都是在虚拟机里测试的。如果你在真机上遇到问题,欢迎[提交 issue](../../issues),
|
除 GNOME(X11)外,其余桌面目前都是在虚拟机里测试的。如果你在真机上遇到问题,欢迎[提交 issue](../../issues),
|
||||||
请附上发行版、桌面环境、显卡型号和 `journalctl --user -u uuremote-native.service` 里的相关日志。
|
请附上发行版、桌面环境、显卡型号和 `journalctl --user -u uuremote-native.service` 里的相关日志。
|
||||||
|
|
||||||
Wayland 下的限制:只支持单屏、固定分辨率;portal 后端暂不支持剪贴板、唤醒屏幕和动态分辨率。
|
Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端默认使用双缓冲流水线 shm screencopy,控制端帧率设置会同步到采集节拍和编码器,但实际帧率仍受合成器、渲染和读回速度限制(旧串行路径在 2560x1600 曾测得约 16~22 帧;流水线只在虚拟机里测量过,不代表实机保证);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 帧。
|
||||||
首次连接时,桌面会弹窗请求「远程桌面 / 屏幕共享」授权,同意后会记住授权。
|
首次连接时,桌面会弹窗请求「远程桌面 / 屏幕共享」授权,同意后会记住授权。
|
||||||
|
|
||||||
## 尚未实现 / 不支持
|
## 尚未实现 / 不支持
|
||||||
@@ -70,8 +80,7 @@ Wayland 下的限制:只支持单屏、固定分辨率;portal 后端暂不
|
|||||||
| 被控端防窥模式 | 未实现 |
|
| 被控端防窥模式 | 未实现 |
|
||||||
| 麦克风(控制端声音传到被控端) | 未实现(需要虚拟声卡) |
|
| 麦克风(控制端声音传到被控端) | 未实现(需要虚拟声卡) |
|
||||||
| 魔兽世界排队提醒等插件 | 不支持,会明确告诉客户端「无插件」 |
|
| 魔兽世界排队提醒等插件 | 不支持,会明确告诉客户端「无插件」 |
|
||||||
| 登录前(登录界面 / 无人登录)访问 | 不支持,只能控制已登录的桌面会话 |
|
| Linux 系统登录界面 | 还不支持控制开机后的系统登录界面(还没有人登录 Linux 桌面时);这种情况可以用无头模式或虚拟桌面(见下文)。注意:这里说的是 Linux 系统登录,**不登录 UU 账号**现在可以通过「远程协助」使用 |
|
||||||
| 端口映射 | 有代码,未经验收 |
|
|
||||||
| 跨运营商 / 受限网络的中继连接 | 已接入官方下发的 ICE 服务器,但复杂网络环境未充分测试 |
|
| 跨运营商 / 受限网络的中继连接 | 已接入官方下发的 ICE 服务器,但复杂网络环境未充分测试 |
|
||||||
| 作为控制端(在 Linux 上控制别的电脑) | 不在本项目范围 |
|
| 作为控制端(在 Linux 上控制别的电脑) | 不在本项目范围 |
|
||||||
|
|
||||||
@@ -142,6 +151,66 @@ journalctl --user -u uuremote-native.service -f # 看到 availability=Some("re
|
|||||||
|
|
||||||
如果还没有登录,服务会自动在桌面上弹出扫码窗口。
|
如果还没有登录,服务会自动在桌面上弹出扫码窗口。
|
||||||
|
|
||||||
|
### 5. Wayland 下使用 uinput 虚拟键鼠(推荐)
|
||||||
|
|
||||||
|
Wayland(尤其是 GNOME/KDE portal)下,默认的键鼠注入要经过 portal 授权,部分键盘布局下还会丢符号(比如下划线)。
|
||||||
|
允许当前登录用户访问 `/dev/uinput` 后,服务会自动改用内核级虚拟键盘和鼠标,发送物理按键码:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
echo 'KERNEL=="uinput", SUBSYSTEM=="misc", OPTIONS+="static_node=uinput", TAG+="uaccess"' | \
|
||||||
|
sudo tee /etc/udev/rules.d/60-uu-remote-linux-uinput.rules
|
||||||
|
sudo udevadm control --reload && sudo udevadm trigger --name-match=uinput
|
||||||
|
```
|
||||||
|
|
||||||
|
`uaccess` 只把权限给当前坐在这台机器前的登录用户。`UU_INPUT_BACKEND=uinput|native` 可以强制指定后端。
|
||||||
|
X11 默认继续用 XTest,因为它能正确处理网络延迟下按住不放的按键。
|
||||||
|
|
||||||
|
## 远程协助与主窗口(实验性)
|
||||||
|
|
||||||
|
和官方客户端的「远程协助」一样:本机显示**设备 ID** 和**验证码**,把它们发给信任的人,对方在 UU 远程的「开始协助」里输入即可控制本机。
|
||||||
|
|
||||||
|
- **未登录**:启动后直接显示主窗口,用访客身份提供远程协助,同时显示扫码登录二维码。扫码登录后自动切换到账号模式。
|
||||||
|
- **已登录**:本机出现在你的设备列表里,可以直接连;远程协助也照常可用(同一设备 ID)。服务在后台运行时窗口默认隐藏,用 `uuremote-server show` 或应用菜单里的「UU 远程 Linux 主机」打开。关闭窗口只是隐藏,不会停止服务。
|
||||||
|
- **托盘图标**:后台运行时顶栏/任务栏有托盘图标,菜单里可以「打开主窗口」、切换「允许他人远程协助」、「退出」。GNOME 需要 AppIndicator 扩展(Ubuntu 自带),Xfce / Cinnamon / KDE 直接可用。从托盘退出会停止 `uuremote-native.service`,之后从应用菜单打开会重新启动服务。
|
||||||
|
- **设置**(与官方一致):「允许他人远程协助」开关(默认关闭);验证方式:仅临时验证码 / 仅自定义验证码(8–16 位,需同时包含字母和数字) / 两种都可以;临时验证码自动刷新:每次远控结束 / 每小时 / 每 12 小时 / 每 24 小时 / 不刷新;ID 和验证码旁各有「复制」按钮。设置保存在本机配置文件(0600)。
|
||||||
|
- **只在本机窗口显示**:验证码不会写进日志。没有图形界面时,单独运行 `uuremote-server assist` 会在终端显示(`allow` / `deny` / `reset` / `mode …` / `refresh …` / `off`)。
|
||||||
|
- **安全**:拿到 ID 和验证码的人可以完全控制这台电脑(桌面、文件、终端)。只发给信任的人,用完关掉开关或刷新验证码。
|
||||||
|
|
||||||
|
安装应用菜单图标(可选):
|
||||||
|
|
||||||
|
```sh
|
||||||
|
install -Dm644 contrib/desktop/uu-remote-linux.svg ~/.local/share/icons/hicolor/scalable/apps/uu-remote-linux.svg
|
||||||
|
install -Dm644 contrib/desktop/uu-remote-linux.desktop ~/.local/share/applications/uu-remote-linux.desktop
|
||||||
|
```
|
||||||
|
|
||||||
|
验证情况:访客身份下的连接、错误验证码拒绝、用完换码、关闭后不可连接、登录与退出登录的切换,已用官方 Mac 客户端实测通过。
|
||||||
|
**登录状态下的远程协助尚未用另一个账号实测**(同一账号的设备不能互相发起远程协助,这是官方限制)。
|
||||||
|
|
||||||
|
## 没有显示器的服务器
|
||||||
|
|
||||||
|
**无头模式**:只要终端、文件传输和端口映射,不需要图形界面。在服务里加 `Environment=UU_DESKTOP_BACKEND=none`,
|
||||||
|
再执行 `loginctl enable-linger $USER`,这样开机后不用登录,systemd 用户服务(以及 `XDG_RUNTIME_DIR`、用户 D-Bus)也会自动启动。
|
||||||
|
无头模式下控制端点「远程桌面」会一直显示正在连接,这是正常的。首次登录用 `uuremote-server login --no-gui` 在终端里扫码。
|
||||||
|
|
||||||
|
**虚拟桌面**:需要完整的远程桌面时,用 Xvfb 创建一块虚拟屏幕,再在上面跑一个桌面环境(推荐 Xfce):
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sudo apt install xvfb xauth x11-utils dbus-x11 dbus-user-session xfce4 # 或其他桌面环境,用 UU_VIRTUAL_SESSION 指定
|
||||||
|
install -Dm755 contrib/virtual-desktop/uu-virtual-desktop ~/.local/bin/uu-virtual-desktop
|
||||||
|
install -Dm644 contrib/virtual-desktop/uu-virtual-desktop.service ~/.config/systemd/user/uu-virtual-desktop.service
|
||||||
|
install -Dm644 contrib/virtual-desktop/virtual-display.conf ~/.config/systemd/user/uuremote-native.service.d/virtual-display.conf
|
||||||
|
loginctl enable-linger $USER
|
||||||
|
systemctl --user daemon-reload && systemctl --user enable --now uu-virtual-desktop.service
|
||||||
|
systemctl --user restart uuremote-native.service
|
||||||
|
```
|
||||||
|
|
||||||
|
虚拟屏幕默认是 `:99`、1920x1080。Xvfb 没有 GPU 加速,流畅度比真实显示器略差;有 NVIDIA 显卡时视频仍然用 NVENC 编码。
|
||||||
|
脚本会把 `DISPLAY` 导入 systemd 用户会话,这样通过 D-Bus 启动的程序也会显示在虚拟屏幕上;
|
||||||
|
如果这台机器同时还有真实的桌面会话,请设 `UU_VIRTUAL_BUS=private`,避免影响真实桌面。
|
||||||
|
|
||||||
|
想让 NVIDIA/AMD 显卡在不接显示器时也正常输出画面(而不是用 Xvfb),可以用假显示器插头(HDMI 诱骗器),
|
||||||
|
或者用内核参数 `drm.edid_firmware=` 加载 EDID 文件,再按普通 X11 桌面的方式安装。
|
||||||
|
|
||||||
## 配置
|
## 配置
|
||||||
|
|
||||||
常用环境变量:
|
常用环境变量:
|
||||||
@@ -149,11 +218,16 @@ journalctl --user -u uuremote-native.service -f # 看到 availability=Some("re
|
|||||||
| 变量 | 作用 |
|
| 变量 | 作用 |
|
||||||
|---|---|
|
|---|---|
|
||||||
| `UU_CONFIG` | 配置文件路径(默认 `~/.config/uuremote/config.json`) |
|
| `UU_CONFIG` | 配置文件路径(默认 `~/.config/uuremote/config.json`) |
|
||||||
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` |
|
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` / `kms`(实验性特性) / `none`(无头模式) |
|
||||||
| `UU_VIDEO_ENCODER=cpu` | 强制使用 CPU 编码 |
|
| `UU_INPUT_BACKEND` | `auto`(默认,Wayland 下可用时用 uinput)/ `uinput` / `native` |
|
||||||
|
| `UU_TERMINAL_SHORTCUTS=0` | 关闭终端窗口里的 Ctrl+V 粘贴转换 |
|
||||||
|
| `UU_VIDEO_ENCODER` | `cpu` 强制 CPU;`vaapi` 显式启用 Intel/AMD 硬编;`nvenc` 强制 NVENC;未设置时检测到 NVIDIA 驱动才用 NVENC,否则用 CPU |
|
||||||
| `UU_FFMPEG_ROOT` | 使用指定目录下的 FFmpeg |
|
| `UU_FFMPEG_ROOT` | 使用指定目录下的 FFmpeg |
|
||||||
| `UU_AUDIO=0` | 关闭声音转发;`UU_AUDIO_SOURCE` 指定 PulseAudio 采集源 |
|
| `UU_AUDIO=0` | 关闭声音转发;`UU_AUDIO_SOURCE` 指定 PulseAudio 采集源 |
|
||||||
| `UU_LOGIN_WINDOW=0` | 凭证缺失时不自动弹出登录窗口 |
|
| `UU_LOGIN_WINDOW=0` | 凭证缺失时不自动弹出登录窗口 |
|
||||||
|
| `UU_REMOTE_ASSIST=0` | 关闭远程协助(未登录时也不再以访客身份提供协助) |
|
||||||
|
| `UU_ASSIST_ALLOW=1` | 启动时就允许他人远程协助(覆盖窗口里保存的开关状态) |
|
||||||
|
| `UU_PORT_MAPPING=0` | 关闭端口映射(对应官方被控端的开关) |
|
||||||
|
|
||||||
「远程结束后自动锁屏」「静音运行」等设置保存在 `~/.config/uuremote/host_settings.json`。
|
「远程结束后自动锁屏」「静音运行」等设置保存在 `~/.config/uuremote/host_settings.json`。
|
||||||
|
|
||||||
@@ -200,3 +274,58 @@ flowchart LR
|
|||||||
- 网易 UU 远程团队:做了一个好用的远程工具。
|
- 网易 UU 远程团队:做了一个好用的远程工具。
|
||||||
- [webrtc-rs](https://github.com/webrtc-rs/webrtc)、FFmpeg、GTK 等开源项目。
|
- [webrtc-rs](https://github.com/webrtc-rs/webrtc)、FFmpeg、GTK 等开源项目。
|
||||||
- 本项目的大部分代码在 Claude 和 Codex 的协助下完成。
|
- 本项目的大部分代码在 Claude 和 Codex 的协助下完成。
|
||||||
|
|
||||||
|
### VAAPI 硬件编码(显式启用)
|
||||||
|
|
||||||
|
设置 `UU_VIDEO_ENCODER=vaapi` 使用 Intel/AMD H.264 VAAPI;`UU_VAAPI_DEVICE` 默认
|
||||||
|
`/dev/dri/renderD128`,运行用户需要 render 节点权限。不会自动选择 VAAPI;设备或驱动初始化
|
||||||
|
失败会明确警告并回退 libx264。外部 FFmpeg 和 `--features inproc-av` 均先转 NV12 再上传硬件帧。
|
||||||
|
VBR 使用控制端码率预设及 CQ 对应的最小 QP,无 B 帧、async depth=1,并保留现有 GOP/VFR 规则。
|
||||||
|
Intel 核显(UHD 770,iHD 驱动)已实机验收:默认的外部 FFmpeg 管线用官方 Mac 控制端测试了各档画质、帧率、缩放和重连,全程硬件编码、无回退,CPU 占用比 libx264 低约 40%。**AMD 显卡和 `--features inproc-av` 管线的 VAAPI 尚未实机验证。**
|
||||||
|
|
||||||
|
### 静音运行与虚拟声卡
|
||||||
|
|
||||||
|
控制端开启「静音运行」后,程序创建虚拟输出 `uu-remote-sink`,保存原默认输出,
|
||||||
|
迁移其播放流并采集 `uu-remote-sink.monitor`。新播放也走虚拟输出;其他输出上的流不迁移。
|
||||||
|
最后一个桌面连接结束后恢复原默认输出并卸载模块;`mute_permanent` 仍保留原物理静音策略。
|
||||||
|
恢复记录保存在配置旁,权限 0600;进程锁避免重复实例抢占,异常退出后下次启动会恢复遗留路由。
|
||||||
|
有 `pactl` 时用它(PulseAudio / PipeWire-pulse);没有 `pactl` 的纯 PipeWire 系统(如默认的 Ubuntu 桌面)
|
||||||
|
自动改用 `pw-cli`/`wpctl` 创建同名虚拟输出,正在播放的声音由 WirePlumber 自动迁移。已在 Ubuntu 24.04 实机上用官方 Mac 控制端验收。
|
||||||
|
路由失败时保留原静音/monitor 方案。开关变化仅重启当前音频编码器,未开启时采集方式保持不变。
|
||||||
|
本地诊断命令 `virtual-audio-probe` 会临时切换输出五秒并恢复。
|
||||||
|
|
||||||
|
### 虚拟桌面的用户总线
|
||||||
|
|
||||||
|
默认 `UU_VIRTUAL_BUS=user`:监督脚本将 DISPLAY/XAUTHORITY 导入 linger 用户总线,等待经过认证的
|
||||||
|
X11 就绪后通知 systemd;退出时清理 Xvfb。示例主机 drop-in 使用 `%t/uu-virtual-99.Xauthority`。
|
||||||
|
请使用没有真实图形会话的专用账号;参见[设置与官方 Mac 验收清单](contrib/virtual-desktop/ACCEPTANCE.md)。
|
||||||
|
|
||||||
|
### Arch Linux / Omarchy
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sudo pacman -S --needed base-devel rust pkgconf clang ffmpeg libx11 libxext libxtst libxfixes gtk3 \
|
||||||
|
wayland libxkbcommon wl-clipboard libpipewire glib2 libpulse
|
||||||
|
cargo test --locked --features wayland
|
||||||
|
cargo build --release --features wayland
|
||||||
|
```
|
||||||
|
|
||||||
|
Omarchy 使用 Hyprland:在其用户会话中运行,设置 `UU_DESKTOP_BACKEND=wayland`,保留
|
||||||
|
`WAYLAND_DISPLAY` 和 `XDG_RUNTIME_DIR`。其他合成器可能需要 `--features wayland,portal`。
|
||||||
|
uinput 需加载内核模块并应用上文 uaccess 规则;无人登录账号需要专用组的受限授权。
|
||||||
|
该权限能向整个 seat(包括锁屏/登录界面)注入输入。设置 `UU_INPUT_BACKEND=uinput` 后,
|
||||||
|
仍须确认启动日志实际选择了 uinput。
|
||||||
|
|
||||||
|
VAAPI 可选包:Intel 新显卡用 `intel-media-driver`,AMD 用 `libva-mesa-driver`,`libva-utils` 提供
|
||||||
|
`vainfo`。KMS 依赖 `libdrm`。虚拟 Xfce 依赖 `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
|
||||||
|
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`。
|
||||||
|
滚动 Arch 的 FFmpeg 可能超出 `inproc-av` 支持的 6.x;请使用默认进程方案或独立的 FFmpeg 6 前缀,
|
||||||
|
不要降级系统 FFmpeg。
|
||||||
|
|
||||||
|
### 实验性 KMS 截屏
|
||||||
|
|
||||||
|
编译 `--features kms` 后显式设置 `UU_DESKTOP_BACKEND=kms`;`UU_KMS_DEVICE` 默认
|
||||||
|
`/dev/dri/card0`。需要 libdrm、card 节点访问权及主动授予的 `cap_sys_admin+ep`。
|
||||||
|
程序不会自动启用或授权;此 capability 权限很大,此 capability 权限很大:它作用于整个联网的服务进程,也能看到登录界面/锁屏等原本受授权保护的画面;只在可丢弃的机器上、对审查过的二进制使用,用 `getcap` 检查,`sudo setcap -r <路径>` 撤销。目前在虚拟机里登录界面截图全黑,不支持锁屏/登录界面。
|
||||||
|
当前仅为线性帧缓冲原型:VM 的 GDM 截图仍是黑屏,**没有通过登录界面视觉验收**,不承诺锁屏可用。
|
||||||
|
键鼠需 uinput;没有桌面会话时不提供剪贴板。
|
||||||
|
无头模式下桌面连接没有找到官方的「无桌面」错误码,因此保持原有 offer 行为。
|
||||||
@@ -2,13 +2,23 @@ fn main() {
|
|||||||
println!("cargo:rerun-if-changed=c_engine/portal_clipboard.inc");
|
println!("cargo:rerun-if-changed=c_engine/portal_clipboard.inc");
|
||||||
println!("cargo:rerun-if-changed=c_engine/x11_engine.c");
|
println!("cargo:rerun-if-changed=c_engine/x11_engine.c");
|
||||||
println!("cargo:rerun-if-changed=c_engine/clipboard.c");
|
println!("cargo:rerun-if-changed=c_engine/clipboard.c");
|
||||||
println!("cargo:rerun-if-changed=c_engine/login_window.c");
|
println!("cargo:rerun-if-changed=c_engine/assist_window.c");
|
||||||
println!("cargo:rerun-if-changed=c_engine/pam_check.c");
|
println!("cargo:rerun-if-changed=c_engine/pam_check.c");
|
||||||
println!("cargo:rerun-if-env-changed=UU_NO_GTK");
|
println!("cargo:rerun-if-env-changed=UU_NO_GTK");
|
||||||
println!("cargo:rustc-check-cfg=cfg(uu_gtk)");
|
println!("cargo:rustc-check-cfg=cfg(uu_gtk)");
|
||||||
|
|
||||||
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
|
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
|
||||||
if target_os == "linux" {
|
if target_os == "linux" {
|
||||||
|
if std::env::var_os("CARGO_FEATURE_KMS").is_some() {
|
||||||
|
println!("cargo:rerun-if-changed=c_engine/kms_engine.c");
|
||||||
|
let output=std::process::Command::new("pkg-config").args(["--cflags","libdrm"]).output().expect("KMS requires pkg-config and libdrm development headers");
|
||||||
|
assert!(output.status.success(), "KMS requires libdrm development headers");
|
||||||
|
let mut build=cc::Build::new();
|
||||||
|
build.file("c_engine/kms_engine.c");
|
||||||
|
for flag in String::from_utf8_lossy(&output.stdout).split_whitespace() { build.flag(flag); }
|
||||||
|
build.compile("uu_kms_engine");
|
||||||
|
println!("cargo:rustc-link-lib=drm");
|
||||||
|
}
|
||||||
cc::Build::new()
|
cc::Build::new()
|
||||||
.file("c_engine/x11_engine.c")
|
.file("c_engine/x11_engine.c")
|
||||||
.file("c_engine/clipboard.c")
|
.file("c_engine/clipboard.c")
|
||||||
@@ -26,7 +36,7 @@ fn main() {
|
|||||||
match (std::env::var_os("UU_NO_GTK").is_none()).then(|| gtk("--cflags").zip(gtk("--libs"))).flatten() {
|
match (std::env::var_os("UU_NO_GTK").is_none()).then(|| gtk("--cflags").zip(gtk("--libs"))).flatten() {
|
||||||
Some((cflags, libs)) => {
|
Some((cflags, libs)) => {
|
||||||
let mut build = cc::Build::new();
|
let mut build = cc::Build::new();
|
||||||
build.file("c_engine/login_window.c").flag_if_supported("-Wno-deprecated-declarations");
|
build.file("c_engine/assist_window.c").flag_if_supported("-Wno-deprecated-declarations");
|
||||||
for flag in cflags.split_whitespace() { build.flag(flag); }
|
for flag in cflags.split_whitespace() { build.flag(flag); }
|
||||||
build.compile("uu_login_window");
|
build.compile("uu_login_window");
|
||||||
for flag in libs.split_whitespace() {
|
for flag in libs.split_whitespace() {
|
||||||
@@ -62,7 +72,7 @@ fn main() {
|
|||||||
let mut build = cc::Build::new();
|
let mut build = cc::Build::new();
|
||||||
build.flag_if_supported("-Wno-unused-parameter"); // protocol listener signatures
|
build.flag_if_supported("-Wno-unused-parameter"); // protocol listener signatures
|
||||||
build.file("c_engine/wayland_engine.c").include(&out);
|
build.file("c_engine/wayland_engine.c").include(&out);
|
||||||
for name in ["wlr-screencopy-unstable-v1", "wlr-virtual-pointer-unstable-v1", "virtual-keyboard-unstable-v1", "wlr-output-power-management-unstable-v1"] {
|
for name in ["wlr-screencopy-unstable-v1", "wlr-virtual-pointer-unstable-v1", "virtual-keyboard-unstable-v1", "wlr-output-power-management-unstable-v1", "linux-dmabuf-unstable-v1"] {
|
||||||
let xml = format!("c_engine/wayland-protocols/{name}.xml");
|
let xml = format!("c_engine/wayland-protocols/{name}.xml");
|
||||||
println!("cargo:rerun-if-changed={xml}");
|
println!("cargo:rerun-if-changed={xml}");
|
||||||
for (mode, suffix) in [("client-header", "client-protocol.h"), ("private-code", "protocol.c")] {
|
for (mode, suffix) in [("client-header", "client-protocol.h"), ("private-code", "protocol.c")] {
|
||||||
@@ -80,4 +90,3 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,644 @@
|
|||||||
|
// GTK3 window for guest remote assistance (`uuremote-server assist`). Layout follows the official
|
||||||
|
// "本设备" card (allow switch, device ID, temporary code with show/hide, copy & share) without any
|
||||||
|
// NetEase assets. GTK runs on its own thread; Rust pushes state through g_idle_add and polls the
|
||||||
|
// user's commands. The code is only drawn in this local window and copied on explicit request.
|
||||||
|
#include <gtk/gtk.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <stdatomic.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <dlfcn.h>
|
||||||
|
|
||||||
|
static const char *CSS =
|
||||||
|
"window { background-color: #f5f6f8; }\n"
|
||||||
|
"* { font-family: 'Noto Sans CJK SC', 'Noto Sans SC', sans-serif; }\n"
|
||||||
|
".card { background-color: #ffffff; border-radius: 10px; border: 1px solid #e5e7eb; }\n"
|
||||||
|
".heading { font-size: 20px; font-weight: 700; color: #1d1d1f; }\n"
|
||||||
|
".key { font-size: 14px; color: #4b5563; }\n"
|
||||||
|
".big { font-size: 32px; font-weight: 800; color: #111111; }\n"
|
||||||
|
".note { font-size: 13px; color: #9ca3af; }\n"
|
||||||
|
".hint { font-size: 13px; color: #6b7280; }\n"
|
||||||
|
".footer { font-size: 12px; color: #9ca3af; }\n"
|
||||||
|
"button.flat { padding: 4px 8px; }\n"
|
||||||
|
"button.share { background-image: none; background-color: #ffffff; border: 1px solid #d1d5db;"
|
||||||
|
" border-radius: 6px; padding: 6px 14px; font-size: 14px; }\n"
|
||||||
|
"separator { background-color: #e5e7eb; min-height: 1px; }\n"
|
||||||
|
".user { font-size: 22px; font-weight: 700; color: #111111; }\n"
|
||||||
|
".status { font-size: 13px; color: #2f6bff; }\n"
|
||||||
|
".status.ok { color: #16a34a; }\n"
|
||||||
|
".status.error { color: #dc2626; }\n";
|
||||||
|
|
||||||
|
enum { CMD_NONE = 0, CMD_ALLOW = 1, CMD_DENY = 2, CMD_RESET = 3, CMD_STOP = 4, CMD_VERIFY = 5, CMD_REFRESH = 6, CMD_CUSTOM = 7, CMD_LOGOUT = 8, CMD_QUIT = 9 };
|
||||||
|
enum { VERIFY_TEMPORARY = 0, VERIFY_CUSTOM = 1, VERIFY_BOTH = 2 };
|
||||||
|
|
||||||
|
typedef struct { char *id; char *code; int allowed, verify, refresh, has_custom; } State;
|
||||||
|
typedef struct { int cmd; char arg[40]; } Cmd;
|
||||||
|
|
||||||
|
// Small FIFO so quick successive UI actions (e.g. set code, then mode) are all delivered.
|
||||||
|
static Cmd queue[16];
|
||||||
|
static int q_head, q_len;
|
||||||
|
static pthread_mutex_t q_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
static atomic_int closed;
|
||||||
|
static pthread_t ui_thread;
|
||||||
|
static int ui_started, init_state; // 0 pending, 1 ok, -1 failed
|
||||||
|
static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
static pthread_cond_t init_cond = PTHREAD_COND_INITIALIZER;
|
||||||
|
static GtkWidget *window, *allow_switch, *id_label, *code_label, *eye_button, *share_button, *hint_label, *verify_combo, *note_label;
|
||||||
|
static GtkWidget *refresh_items[5], *footer_label;
|
||||||
|
// Account card: QR login before sign-in, user info (+ logout) after.
|
||||||
|
static GtkWidget *assist_card, *account_card, *account_stack, *qr_stack, *qr_image, *login_status, *user_label;
|
||||||
|
static int show_assist = 1, show_account;
|
||||||
|
// Tray (host service only): AppIndicator via dlopen (no build dependency), else GtkStatusIcon.
|
||||||
|
static GtkWidget *tray_menu, *tray_allow;
|
||||||
|
static int suppress_tray;
|
||||||
|
static int cur_verify, cur_refresh, cur_has_custom;
|
||||||
|
static char current_id[64], current_code[64];
|
||||||
|
static int code_visible = 1, suppress_switch, start_hidden, close_hides;
|
||||||
|
|
||||||
|
static GtkWidget *label(const char *text, const char *cls) {
|
||||||
|
GtkWidget *l = gtk_label_new(text);
|
||||||
|
gtk_style_context_add_class(gtk_widget_get_style_context(l), cls);
|
||||||
|
gtk_widget_set_halign(l, GTK_ALIGN_START);
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void post_arg(int cmd, const char *arg) {
|
||||||
|
pthread_mutex_lock(&q_lock);
|
||||||
|
if (q_len < 16) {
|
||||||
|
Cmd *c = &queue[(q_head + q_len) % 16];
|
||||||
|
c->cmd = cmd; g_strlcpy(c->arg, arg ? arg : "", sizeof c->arg); q_len++;
|
||||||
|
}
|
||||||
|
pthread_mutex_unlock(&q_lock);
|
||||||
|
}
|
||||||
|
static void post(int cmd) { post_arg(cmd, NULL); }
|
||||||
|
static void post_int(int cmd, int v) { char b[8]; g_snprintf(b, sizeof b, "%d", v); post_arg(cmd, b); }
|
||||||
|
|
||||||
|
// 8-16 ASCII letters/digits including both, as the official custom code dialog requires.
|
||||||
|
static int valid_custom(const char *c) {
|
||||||
|
size_t n = strlen(c); int alpha = 0, digit = 0;
|
||||||
|
if (n < 8 || n > 16) return 0;
|
||||||
|
for (size_t i = 0; i < n; i++) {
|
||||||
|
if (g_ascii_isalpha(c[i])) alpha = 1; else if (g_ascii_isdigit(c[i])) digit = 1; else return 0;
|
||||||
|
}
|
||||||
|
return alpha && digit;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_custom_changed(GtkEditable *e, gpointer ok) {
|
||||||
|
gtk_widget_set_sensitive(GTK_WIDGET(ok), valid_custom(gtk_entry_get_text(GTK_ENTRY(e))));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns 1 when a new custom code was entered and posted.
|
||||||
|
static int ask_custom_code(void) {
|
||||||
|
GtkWidget *d = gtk_dialog_new_with_buttons("设置自定义验证码", GTK_WINDOW(window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||||
|
"取消", GTK_RESPONSE_CANCEL, NULL);
|
||||||
|
GtkWidget *ok = gtk_dialog_add_button(GTK_DIALOG(d), "确定", GTK_RESPONSE_OK);
|
||||||
|
gtk_widget_set_sensitive(ok, FALSE);
|
||||||
|
GtkWidget *area = gtk_dialog_get_content_area(GTK_DIALOG(d));
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(area), 16);
|
||||||
|
gtk_box_set_spacing(GTK_BOX(area), 8);
|
||||||
|
GtkWidget *entry = gtk_entry_new();
|
||||||
|
gtk_entry_set_max_length(GTK_ENTRY(entry), 16);
|
||||||
|
gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
|
||||||
|
gtk_entry_set_placeholder_text(GTK_ENTRY(entry), "请输入自定义验证码");
|
||||||
|
g_signal_connect(entry, "changed", G_CALLBACK(on_custom_changed), ok);
|
||||||
|
gtk_box_pack_start(GTK_BOX(area), entry, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(area), label("8-16 位,需同时包含字母和数字;不会自动刷新,请妥善保管", "note"), FALSE, FALSE, 0);
|
||||||
|
gtk_widget_show_all(d);
|
||||||
|
int done = 0;
|
||||||
|
if (gtk_dialog_run(GTK_DIALOG(d)) == GTK_RESPONSE_OK) {
|
||||||
|
const char *code = gtk_entry_get_text(GTK_ENTRY(entry));
|
||||||
|
if (valid_custom(code)) { post_arg(CMD_CUSTOM, code); done = 1; }
|
||||||
|
}
|
||||||
|
gtk_widget_destroy(d);
|
||||||
|
return done;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int suppress_combo;
|
||||||
|
static void on_verify_changed(GtkComboBox *c, gpointer d) {
|
||||||
|
(void)d;
|
||||||
|
if (suppress_combo) return;
|
||||||
|
int v = gtk_combo_box_get_active(c);
|
||||||
|
if (v != VERIFY_TEMPORARY && !cur_has_custom && !ask_custom_code()) {
|
||||||
|
suppress_combo = 1; gtk_combo_box_set_active(c, cur_verify); suppress_combo = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
post_int(CMD_VERIFY, v);
|
||||||
|
}
|
||||||
|
static void on_set_custom(GtkButton *b, gpointer d) { (void)b; (void)d; ask_custom_code(); }
|
||||||
|
|
||||||
|
static int suppress_refresh;
|
||||||
|
static void on_refresh_item(GtkCheckMenuItem *item, gpointer data) {
|
||||||
|
if (suppress_refresh || !gtk_check_menu_item_get_active(item)) return;
|
||||||
|
post_int(CMD_REFRESH, GPOINTER_TO_INT(data));
|
||||||
|
}
|
||||||
|
static void on_refresh_now(GtkMenuItem *i, gpointer d) { (void)i; (void)d; post(CMD_RESET); }
|
||||||
|
|
||||||
|
static const char *REFRESH_NOTES[5] = { "每次远控结束刷新", "每小时自动刷新", "每 12 小时自动刷新", "每 24 小时自动刷新", "不自动刷新" };
|
||||||
|
|
||||||
|
static void render_code(void) {
|
||||||
|
if (!code_label) return;
|
||||||
|
gtk_label_set_text(GTK_LABEL(code_label), current_code[0] ? (code_visible ? current_code : "********") : "--------");
|
||||||
|
gtk_button_set_label(GTK_BUTTON(eye_button), code_visible ? "隐藏" : "显示");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void copy_text(const char *text, const char *hint) {
|
||||||
|
if (!text || !text[0]) return;
|
||||||
|
gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), text, -1);
|
||||||
|
gtk_label_set_text(GTK_LABEL(hint_label), hint);
|
||||||
|
}
|
||||||
|
// The ID is copied without the display spaces so it pastes straight into the viewer's ID field.
|
||||||
|
static void on_copy_id(GtkButton *b, gpointer d) {
|
||||||
|
(void)b; (void)d;
|
||||||
|
char digits[64]; size_t n = 0;
|
||||||
|
for (const char *p = current_id; *p && n + 1 < sizeof digits; p++) if (*p != ' ') digits[n++] = *p;
|
||||||
|
digits[n] = 0;
|
||||||
|
copy_text(digits, "已复制设备 ID");
|
||||||
|
}
|
||||||
|
static void on_copy_code(GtkButton *b, gpointer d) { (void)b; (void)d; copy_text(current_code, "已复制验证码,请只发给信任的人"); }
|
||||||
|
|
||||||
|
static void on_eye(GtkButton *b, gpointer d) { (void)b; (void)d; code_visible = !code_visible; render_code(); }
|
||||||
|
|
||||||
|
static void on_share(GtkButton *b, gpointer d) {
|
||||||
|
(void)b; (void)d;
|
||||||
|
if (!current_id[0] || !current_code[0]) return;
|
||||||
|
char *text = g_strdup_printf("UU 远程协助\n设备 ID:%s\n验证码:%s", current_id, current_code);
|
||||||
|
gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), text, -1);
|
||||||
|
g_free(text);
|
||||||
|
gtk_label_set_text(GTK_LABEL(hint_label), "已复制设备 ID 和验证码,请只发给信任的人");
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean on_switch(GtkSwitch *s, gboolean state, gpointer d) {
|
||||||
|
(void)s; (void)d;
|
||||||
|
if (!suppress_switch) post(state ? CMD_ALLOW : CMD_DENY);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Host service: the close button only hides the window (assistance keeps its own on/off switch).
|
||||||
|
static gboolean on_delete(GtkWidget *w, GdkEvent *e, gpointer d) {
|
||||||
|
(void)e; (void)d;
|
||||||
|
if (!close_hides) return FALSE;
|
||||||
|
gtk_widget_hide(w);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_destroy(GtkWidget *w, gpointer d) {
|
||||||
|
(void)w; (void)d;
|
||||||
|
window = code_label = NULL;
|
||||||
|
atomic_store(&closed, 1);
|
||||||
|
post(CMD_STOP);
|
||||||
|
gtk_main_quit();
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean draw_avatar(GtkWidget *w, cairo_t *cr, gpointer d) {
|
||||||
|
(void)w; (void)d;
|
||||||
|
double s = 64;
|
||||||
|
cairo_arc(cr, s / 2, s / 2, s / 2 - 1, 0, 2 * G_PI);
|
||||||
|
cairo_set_source_rgb(cr, 0.84, 0.90, 1.0); cairo_fill_preserve(cr);
|
||||||
|
cairo_set_source_rgb(cr, 0.36, 0.55, 1.0); cairo_set_line_width(cr, 2); cairo_stroke(cr);
|
||||||
|
cairo_save(cr);
|
||||||
|
cairo_arc(cr, s / 2, s / 2, s / 2 - 2, 0, 2 * G_PI); cairo_clip(cr);
|
||||||
|
cairo_set_source_rgb(cr, 0.27, 0.51, 1.0);
|
||||||
|
cairo_arc(cr, s / 2, s * 0.38, s * 0.16, 0, 2 * G_PI); cairo_fill(cr);
|
||||||
|
cairo_arc(cr, s / 2, s * 0.92, s * 0.32, G_PI, 2 * G_PI); cairo_fill(cr);
|
||||||
|
cairo_restore(cr);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
static void on_logout(GtkButton *b, gpointer d) {
|
||||||
|
(void)b; (void)d;
|
||||||
|
GtkWidget *dlg = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
|
||||||
|
"退出登录后,本机将从你的设备列表中下线,远程协助会改用访客身份。确定退出吗?");
|
||||||
|
if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) {
|
||||||
|
post(CMD_LOGOUT);
|
||||||
|
gtk_label_set_text(GTK_LABEL(user_label), "正在退出登录…");
|
||||||
|
}
|
||||||
|
gtk_widget_destroy(dlg);
|
||||||
|
}
|
||||||
|
|
||||||
|
static GtkWidget *build_account_card(void) {
|
||||||
|
GtkWidget *card = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
||||||
|
gtk_style_context_add_class(gtk_widget_get_style_context(card), "card");
|
||||||
|
GtkWidget *header = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(header), 20);
|
||||||
|
gtk_box_pack_start(GTK_BOX(header), label("账号", "heading"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(card), header, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(card), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE, FALSE, 0);
|
||||||
|
account_stack = gtk_stack_new();
|
||||||
|
gtk_stack_set_transition_type(GTK_STACK(account_stack), GTK_STACK_TRANSITION_TYPE_CROSSFADE);
|
||||||
|
gtk_stack_set_vhomogeneous(GTK_STACK(account_stack), FALSE); // user page is shorter than the QR page
|
||||||
|
|
||||||
|
// Not signed in: QR code + guidance.
|
||||||
|
GtkWidget *qr_page = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 24);
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(qr_page), 20);
|
||||||
|
qr_stack = gtk_stack_new();
|
||||||
|
gtk_widget_set_size_request(qr_stack, 180, 180);
|
||||||
|
GtkWidget *loading = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
|
||||||
|
gtk_widget_set_valign(loading, GTK_ALIGN_CENTER);
|
||||||
|
GtkWidget *spinner = gtk_spinner_new();
|
||||||
|
gtk_spinner_start(GTK_SPINNER(spinner));
|
||||||
|
gtk_box_pack_start(GTK_BOX(loading), spinner, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(loading), label("正在获取二维码…", "note"), FALSE, FALSE, 0);
|
||||||
|
gtk_stack_add_named(GTK_STACK(qr_stack), loading, "loading");
|
||||||
|
qr_image = gtk_image_new();
|
||||||
|
gtk_stack_add_named(GTK_STACK(qr_stack), qr_image, "qr");
|
||||||
|
gtk_box_pack_start(GTK_BOX(qr_page), qr_stack, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *qr_text = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
|
||||||
|
gtk_widget_set_valign(qr_text, GTK_ALIGN_CENTER);
|
||||||
|
gtk_box_pack_start(GTK_BOX(qr_text), label("扫码登录", "heading"), FALSE, FALSE, 0);
|
||||||
|
GtkWidget *cap = label("请使用网易 UU 远程 App 扫一扫登录。\n登录后本机会出现在你的设备列表中,可直接远程控制;\n远程协助在登录前后都可以使用。", "hint");
|
||||||
|
gtk_label_set_xalign(GTK_LABEL(cap), 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(qr_text), cap, FALSE, FALSE, 0);
|
||||||
|
login_status = label("", "status");
|
||||||
|
gtk_label_set_line_wrap(GTK_LABEL(login_status), TRUE);
|
||||||
|
gtk_box_pack_start(GTK_BOX(qr_text), login_status, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(qr_page), qr_text, TRUE, TRUE, 0);
|
||||||
|
gtk_stack_add_named(GTK_STACK(account_stack), qr_page, "qr");
|
||||||
|
|
||||||
|
// Signed in: avatar + account name + logout.
|
||||||
|
GtkWidget *user_page = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 16);
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(user_page), 20);
|
||||||
|
GtkWidget *avatar = gtk_drawing_area_new();
|
||||||
|
gtk_widget_set_size_request(avatar, 64, 64);
|
||||||
|
gtk_widget_set_valign(avatar, GTK_ALIGN_CENTER);
|
||||||
|
g_signal_connect(avatar, "draw", G_CALLBACK(draw_avatar), NULL);
|
||||||
|
gtk_box_pack_start(GTK_BOX(user_page), avatar, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *user_text = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4);
|
||||||
|
gtk_widget_set_valign(user_text, GTK_ALIGN_CENTER);
|
||||||
|
user_label = label("", "user");
|
||||||
|
gtk_box_pack_start(GTK_BOX(user_text), user_label, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(user_text), label("已登录 · 本机已出现在你的设备列表中", "hint"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(user_page), user_text, TRUE, TRUE, 0);
|
||||||
|
GtkWidget *logout = gtk_button_new_with_label("退出登录");
|
||||||
|
gtk_style_context_add_class(gtk_widget_get_style_context(logout), "share");
|
||||||
|
gtk_widget_set_valign(logout, GTK_ALIGN_CENTER);
|
||||||
|
g_signal_connect(logout, "clicked", G_CALLBACK(on_logout), NULL);
|
||||||
|
gtk_box_pack_end(GTK_BOX(user_page), logout, FALSE, FALSE, 0);
|
||||||
|
gtk_stack_add_named(GTK_STACK(account_stack), user_page, "user");
|
||||||
|
|
||||||
|
gtk_box_pack_start(GTK_BOX(card), account_stack, FALSE, FALSE, 0);
|
||||||
|
return card;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void present_window(void) { if (window) { gtk_widget_show(window); gtk_window_present(GTK_WINDOW(window)); } }
|
||||||
|
static void on_tray_open(GtkMenuItem *i, gpointer d) { (void)i; (void)d; present_window(); }
|
||||||
|
static void on_tray_allow(GtkCheckMenuItem *i, gpointer d) {
|
||||||
|
(void)d;
|
||||||
|
if (!suppress_tray) post(gtk_check_menu_item_get_active(i) ? CMD_ALLOW : CMD_DENY);
|
||||||
|
}
|
||||||
|
static void on_tray_quit(GtkMenuItem *i, gpointer d) {
|
||||||
|
(void)i; (void)d;
|
||||||
|
GtkWidget *dlg = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
|
||||||
|
"退出后本机将下线:设备列表和远程协助都无法连接,直到再次启动。确定退出吗?");
|
||||||
|
gtk_window_set_title(GTK_WINDOW(dlg), "UU 远程 Linux 主机");
|
||||||
|
if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) post(CMD_QUIT);
|
||||||
|
gtk_widget_destroy(dlg);
|
||||||
|
}
|
||||||
|
static void on_status_activate(GtkStatusIcon *i, gpointer d) { (void)i; (void)d; present_window(); }
|
||||||
|
static void on_status_menu(GtkStatusIcon *i, guint button, guint time, gpointer d) {
|
||||||
|
(void)i; (void)button; (void)time; (void)d;
|
||||||
|
gtk_menu_popup_at_pointer(GTK_MENU(tray_menu), NULL);
|
||||||
|
}
|
||||||
|
static void build_tray(void) {
|
||||||
|
tray_menu = gtk_menu_new();
|
||||||
|
GtkWidget *open = gtk_menu_item_new_with_label("打开主窗口");
|
||||||
|
g_signal_connect(open, "activate", G_CALLBACK(on_tray_open), NULL);
|
||||||
|
tray_allow = gtk_check_menu_item_new_with_label("允许他人远程协助");
|
||||||
|
g_signal_connect(tray_allow, "toggled", G_CALLBACK(on_tray_allow), NULL);
|
||||||
|
GtkWidget *quit = gtk_menu_item_new_with_label("退出");
|
||||||
|
g_signal_connect(quit, "activate", G_CALLBACK(on_tray_quit), NULL);
|
||||||
|
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), open);
|
||||||
|
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), tray_allow);
|
||||||
|
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), gtk_separator_menu_item_new());
|
||||||
|
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), quit);
|
||||||
|
gtk_widget_show_all(tray_menu);
|
||||||
|
const char *icon = gtk_icon_theme_has_icon(gtk_icon_theme_get_default(), "uu-remote-linux") ? "uu-remote-linux" : "computer";
|
||||||
|
|
||||||
|
// StatusNotifierItem (GNOME AppIndicator extension, KDE): libayatana-appindicator3 / libappindicator3.
|
||||||
|
const char *libs[] = { "libayatana-appindicator3.so.1", "libappindicator3.so.1" };
|
||||||
|
for (int i = 0; i < 2; i++) {
|
||||||
|
void *h = dlopen(libs[i], RTLD_LAZY | RTLD_LOCAL);
|
||||||
|
if (!h) continue;
|
||||||
|
void *(*ind_new)(const char *, const char *, int) = (void *(*)(const char *, const char *, int))dlsym(h, "app_indicator_new");
|
||||||
|
void (*set_status)(void *, int) = (void (*)(void *, int))dlsym(h, "app_indicator_set_status");
|
||||||
|
void (*set_menu)(void *, GtkMenu *) = (void (*)(void *, GtkMenu *))dlsym(h, "app_indicator_set_menu");
|
||||||
|
void (*set_title)(void *, const char *) = (void (*)(void *, const char *))dlsym(h, "app_indicator_set_title");
|
||||||
|
if (ind_new && set_status && set_menu) {
|
||||||
|
void *ind = ind_new("uu-remote-linux", icon, 0 /* APPLICATION_STATUS */);
|
||||||
|
if (set_title) set_title(ind, "UU 远程 Linux 主机");
|
||||||
|
set_menu(ind, GTK_MENU(tray_menu));
|
||||||
|
set_status(ind, 1 /* ACTIVE */);
|
||||||
|
return; // keep the library loaded for the indicator's lifetime
|
||||||
|
}
|
||||||
|
dlclose(h);
|
||||||
|
}
|
||||||
|
// XEmbed system tray (Xfce, Cinnamon, MATE...).
|
||||||
|
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
||||||
|
GtkStatusIcon *status = gtk_status_icon_new_from_icon_name(icon);
|
||||||
|
gtk_status_icon_set_tooltip_text(status, "UU 远程 Linux 主机");
|
||||||
|
gtk_status_icon_set_title(status, "UU 远程 Linux 主机");
|
||||||
|
g_signal_connect(status, "activate", G_CALLBACK(on_status_activate), NULL);
|
||||||
|
g_signal_connect(status, "popup-menu", G_CALLBACK(on_status_menu), NULL);
|
||||||
|
G_GNUC_END_IGNORE_DEPRECATIONS
|
||||||
|
}
|
||||||
|
|
||||||
|
static void build_window(void) {
|
||||||
|
GtkCssProvider *css = gtk_css_provider_new();
|
||||||
|
gtk_css_provider_load_from_data(css, CSS, -1, NULL);
|
||||||
|
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css),
|
||||||
|
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||||
|
g_object_unref(css);
|
||||||
|
|
||||||
|
gtk_window_set_default_icon_name("uu-remote-linux"); // contrib/desktop icon, when installed
|
||||||
|
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
||||||
|
gtk_window_set_title(GTK_WINDOW(window), "UU 远程 Linux 主机(非官方)");
|
||||||
|
gtk_window_set_default_size(GTK_WINDOW(window), 760, -1);
|
||||||
|
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
|
||||||
|
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
|
||||||
|
g_signal_connect(window, "delete-event", G_CALLBACK(on_delete), NULL);
|
||||||
|
g_signal_connect(window, "destroy", G_CALLBACK(on_destroy), NULL);
|
||||||
|
|
||||||
|
GtkWidget *root = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(root), 24);
|
||||||
|
gtk_container_add(GTK_CONTAINER(window), root);
|
||||||
|
|
||||||
|
GtkWidget *card = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
||||||
|
gtk_style_context_add_class(gtk_widget_get_style_context(card), "card");
|
||||||
|
gtk_box_pack_start(GTK_BOX(root), card, FALSE, FALSE, 0);
|
||||||
|
assist_card = card;
|
||||||
|
|
||||||
|
// Header: 本设备 ... 允许他人远程协助 [switch]
|
||||||
|
GtkWidget *header = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(header), 20);
|
||||||
|
gtk_box_pack_start(GTK_BOX(header), label("本设备", "heading"), FALSE, FALSE, 0);
|
||||||
|
allow_switch = gtk_switch_new();
|
||||||
|
gtk_widget_set_valign(allow_switch, GTK_ALIGN_CENTER);
|
||||||
|
g_signal_connect(allow_switch, "state-set", G_CALLBACK(on_switch), NULL);
|
||||||
|
gtk_box_pack_end(GTK_BOX(header), allow_switch, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_end(GTK_BOX(header), label("允许他人远程协助", "key"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(card), header, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(card), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE, FALSE, 0);
|
||||||
|
|
||||||
|
// Body: ID | code + controls
|
||||||
|
GtkWidget *body = gtk_grid_new();
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(body), 20);
|
||||||
|
gtk_grid_set_column_spacing(GTK_GRID(body), 48);
|
||||||
|
gtk_grid_set_row_spacing(GTK_GRID(body), 6);
|
||||||
|
gtk_grid_attach(GTK_GRID(body), label("本设备 ID", "key"), 0, 0, 1, 1);
|
||||||
|
GtkWidget *id_row = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
|
||||||
|
id_label = label("获取中…", "big");
|
||||||
|
gtk_label_set_selectable(GTK_LABEL(id_label), TRUE);
|
||||||
|
gtk_box_pack_start(GTK_BOX(id_row), id_label, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *copy_id = gtk_button_new_with_label("复制");
|
||||||
|
gtk_style_context_add_class(gtk_widget_get_style_context(copy_id), "flat");
|
||||||
|
gtk_widget_set_valign(copy_id, GTK_ALIGN_CENTER);
|
||||||
|
g_signal_connect(copy_id, "clicked", G_CALLBACK(on_copy_id), NULL);
|
||||||
|
gtk_box_pack_start(GTK_BOX(id_row), copy_id, FALSE, FALSE, 0);
|
||||||
|
gtk_grid_attach(GTK_GRID(body), id_row, 0, 1, 1, 1);
|
||||||
|
|
||||||
|
GtkWidget *verify_row = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
|
||||||
|
gtk_box_pack_start(GTK_BOX(verify_row), label("验证方式:", "key"), FALSE, FALSE, 0);
|
||||||
|
verify_combo = gtk_combo_box_text_new();
|
||||||
|
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(verify_combo), "仅使用临时验证码");
|
||||||
|
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(verify_combo), "仅使用自定义验证码");
|
||||||
|
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(verify_combo), "同时使用两种验证码");
|
||||||
|
gtk_combo_box_set_active(GTK_COMBO_BOX(verify_combo), 0);
|
||||||
|
g_signal_connect(verify_combo, "changed", G_CALLBACK(on_verify_changed), NULL);
|
||||||
|
gtk_box_pack_start(GTK_BOX(verify_row), verify_combo, FALSE, FALSE, 0);
|
||||||
|
gtk_grid_attach(GTK_GRID(body), verify_row, 1, 0, 1, 1);
|
||||||
|
GtkWidget *code_row = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
|
||||||
|
code_label = label("--------", "big");
|
||||||
|
gtk_box_pack_start(GTK_BOX(code_row), code_label, FALSE, FALSE, 0);
|
||||||
|
eye_button = gtk_button_new_with_label("隐藏");
|
||||||
|
gtk_style_context_add_class(gtk_widget_get_style_context(eye_button), "flat");
|
||||||
|
g_signal_connect(eye_button, "clicked", G_CALLBACK(on_eye), NULL);
|
||||||
|
// Gear menu: 立即刷新 / 自动刷新 ▸ (每次远控结束, 每小时, 每 12 小时, 每 24 小时, 关闭) / 设置自定义验证码
|
||||||
|
GtkWidget *menu = gtk_menu_new();
|
||||||
|
GtkWidget *now = gtk_menu_item_new_with_label("立即刷新");
|
||||||
|
g_signal_connect(now, "activate", G_CALLBACK(on_refresh_now), NULL);
|
||||||
|
gtk_menu_shell_append(GTK_MENU_SHELL(menu), now);
|
||||||
|
GtkWidget *auto_item = gtk_menu_item_new_with_label("自动刷新");
|
||||||
|
GtkWidget *sub = gtk_menu_new();
|
||||||
|
const char *names[5] = { "每次远控结束", "每小时", "每 12 小时", "每 24 小时", "关闭" };
|
||||||
|
GSList *group = NULL;
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
refresh_items[i] = gtk_radio_menu_item_new_with_label(group, names[i]);
|
||||||
|
group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(refresh_items[i]));
|
||||||
|
g_signal_connect(refresh_items[i], "toggled", G_CALLBACK(on_refresh_item), GINT_TO_POINTER(i));
|
||||||
|
gtk_menu_shell_append(GTK_MENU_SHELL(sub), refresh_items[i]);
|
||||||
|
}
|
||||||
|
gtk_menu_item_set_submenu(GTK_MENU_ITEM(auto_item), sub);
|
||||||
|
gtk_menu_shell_append(GTK_MENU_SHELL(menu), auto_item);
|
||||||
|
GtkWidget *custom_item = gtk_menu_item_new_with_label("设置自定义验证码…");
|
||||||
|
g_signal_connect(custom_item, "activate", G_CALLBACK(on_set_custom), NULL);
|
||||||
|
gtk_menu_shell_append(GTK_MENU_SHELL(menu), custom_item);
|
||||||
|
gtk_widget_show_all(menu);
|
||||||
|
GtkWidget *gear = gtk_menu_button_new();
|
||||||
|
gtk_button_set_image(GTK_BUTTON(gear), gtk_image_new_from_icon_name("emblem-system-symbolic", GTK_ICON_SIZE_BUTTON));
|
||||||
|
gtk_widget_set_tooltip_text(gear, "刷新设置");
|
||||||
|
gtk_menu_button_set_popup(GTK_MENU_BUTTON(gear), menu);
|
||||||
|
GtkWidget *copy_code = gtk_button_new_with_label("复制");
|
||||||
|
gtk_style_context_add_class(gtk_widget_get_style_context(copy_code), "flat");
|
||||||
|
gtk_widget_set_valign(copy_code, GTK_ALIGN_CENTER);
|
||||||
|
g_signal_connect(copy_code, "clicked", G_CALLBACK(on_copy_code), NULL);
|
||||||
|
gtk_widget_set_valign(eye_button, GTK_ALIGN_CENTER);
|
||||||
|
gtk_widget_set_valign(gear, GTK_ALIGN_CENTER);
|
||||||
|
gtk_box_pack_start(GTK_BOX(code_row), copy_code, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(code_row), eye_button, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(code_row), gear, FALSE, FALSE, 0);
|
||||||
|
gtk_grid_attach(GTK_GRID(body), code_row, 1, 1, 1, 1);
|
||||||
|
note_label = label(REFRESH_NOTES[0], "note");
|
||||||
|
gtk_grid_attach(GTK_GRID(body), note_label, 1, 2, 1, 1);
|
||||||
|
|
||||||
|
share_button = gtk_button_new_with_label("复制并分享");
|
||||||
|
gtk_style_context_add_class(gtk_widget_get_style_context(share_button), "share");
|
||||||
|
gtk_widget_set_valign(share_button, GTK_ALIGN_CENTER);
|
||||||
|
g_signal_connect(share_button, "clicked", G_CALLBACK(on_share), NULL);
|
||||||
|
gtk_grid_attach(GTK_GRID(body), share_button, 2, 1, 1, 1);
|
||||||
|
gtk_box_pack_start(GTK_BOX(card), body, FALSE, FALSE, 0);
|
||||||
|
|
||||||
|
hint_label = label("", "hint");
|
||||||
|
gtk_box_pack_start(GTK_BOX(root), hint_label, FALSE, FALSE, 0);
|
||||||
|
account_card = build_account_card();
|
||||||
|
gtk_widget_set_margin_top(account_card, 6);
|
||||||
|
gtk_box_pack_start(GTK_BOX(root), account_card, FALSE, FALSE, 0);
|
||||||
|
footer_label = label("开源 Linux 被控端 · 非网易官方客户端 · 关闭窗口即停止远程协助", "footer");
|
||||||
|
gtk_box_pack_end(GTK_BOX(root), footer_label, FALSE, FALSE, 0);
|
||||||
|
if (close_hides) gtk_label_set_text(GTK_LABEL(footer_label), "开源 Linux 被控端 · 非网易官方客户端 · 关闭窗口后远程协助仍在后台运行");
|
||||||
|
render_code();
|
||||||
|
if (close_hides) build_tray();
|
||||||
|
gtk_widget_show_all(gtk_bin_get_child(GTK_BIN(window)));
|
||||||
|
gtk_widget_set_visible(assist_card, show_assist);
|
||||||
|
gtk_widget_set_visible(hint_label, show_assist);
|
||||||
|
gtk_widget_set_visible(account_card, show_account);
|
||||||
|
if (!start_hidden) gtk_widget_show(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void *ui_main(void *arg) {
|
||||||
|
(void)arg;
|
||||||
|
int ok = gtk_init_check(NULL, NULL);
|
||||||
|
if (ok) build_window();
|
||||||
|
pthread_mutex_lock(&init_lock);
|
||||||
|
init_state = ok ? 1 : -1;
|
||||||
|
pthread_cond_signal(&init_cond);
|
||||||
|
pthread_mutex_unlock(&init_lock);
|
||||||
|
if (ok) gtk_main();
|
||||||
|
else atomic_store(&closed, 1);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns 0 when the window was created, -1 when no display is usable. hidden: create without
|
||||||
|
// showing (host service, opened later with uu_assist_ui_present); hide_on_close: closing hides.
|
||||||
|
static gboolean apply_sections(gpointer d) {
|
||||||
|
(void)d;
|
||||||
|
if (window) {
|
||||||
|
gtk_widget_set_visible(assist_card, show_assist); gtk_widget_set_visible(hint_label, show_assist);
|
||||||
|
gtk_widget_set_visible(account_card, show_account);
|
||||||
|
}
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
static int ui_start(int hidden, int hide_on_close);
|
||||||
|
int uu_assist_ui_start(int hidden, int hide_on_close) {
|
||||||
|
if (ui_started) { show_assist = 1; g_idle_add(apply_sections, NULL); return init_state == 1 ? 0 : -1; }
|
||||||
|
show_assist = 1;
|
||||||
|
return ui_start(hidden, hide_on_close);
|
||||||
|
}
|
||||||
|
// Reveals the account card on the shared window; standalone `login` opens it with the account card only.
|
||||||
|
int uu_login_ui_start(const char *name, const char *os, const char *cpu, const char *gpu) {
|
||||||
|
(void)name; (void)os; (void)cpu; (void)gpu;
|
||||||
|
show_account = 1;
|
||||||
|
if (ui_started) { g_idle_add(apply_sections, NULL); return init_state == 1 ? 0 : -1; }
|
||||||
|
show_assist = 0;
|
||||||
|
return ui_start(0, 0);
|
||||||
|
}
|
||||||
|
void uu_ui_show_account(void) { show_account = 1; if (ui_started && init_state == 1) g_idle_add(apply_sections, NULL); }
|
||||||
|
static int ui_start(int hidden, int hide_on_close) {
|
||||||
|
if (ui_started) return init_state == 1 ? 0 : -1;
|
||||||
|
start_hidden = hidden; close_hides = hide_on_close;
|
||||||
|
if (pthread_create(&ui_thread, NULL, ui_main, NULL) != 0) return -1;
|
||||||
|
ui_started = 1;
|
||||||
|
pthread_mutex_lock(&init_lock);
|
||||||
|
while (init_state == 0) pthread_cond_wait(&init_cond, &init_lock);
|
||||||
|
pthread_mutex_unlock(&init_lock);
|
||||||
|
if (init_state != 1) { pthread_join(ui_thread, NULL); return -1; }
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean apply_state(gpointer data) {
|
||||||
|
State *s = data;
|
||||||
|
if (window) {
|
||||||
|
g_strlcpy(current_id, s->id, sizeof current_id);
|
||||||
|
g_strlcpy(current_code, s->code, sizeof current_code);
|
||||||
|
gtk_label_set_text(GTK_LABEL(id_label), current_id[0] ? current_id : "获取中…");
|
||||||
|
render_code();
|
||||||
|
suppress_switch = 1;
|
||||||
|
gtk_switch_set_active(GTK_SWITCH(allow_switch), s->allowed);
|
||||||
|
suppress_switch = 0;
|
||||||
|
if (tray_allow) { suppress_tray = 1; gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(tray_allow), s->allowed); suppress_tray = 0; }
|
||||||
|
cur_verify = s->verify; cur_refresh = s->refresh; cur_has_custom = s->has_custom;
|
||||||
|
suppress_combo = 1; gtk_combo_box_set_active(GTK_COMBO_BOX(verify_combo), s->verify); suppress_combo = 0;
|
||||||
|
if (s->refresh >= 0 && s->refresh < 5) {
|
||||||
|
suppress_refresh = 1; gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(refresh_items[s->refresh]), TRUE); suppress_refresh = 0;
|
||||||
|
}
|
||||||
|
gtk_label_set_text(GTK_LABEL(note_label), s->verify == VERIFY_CUSTOM ? "自定义验证码不会自动刷新"
|
||||||
|
: s->verify == VERIFY_BOTH ? "也可使用自定义验证码连接" : REFRESH_NOTES[s->refresh >= 0 && s->refresh < 5 ? s->refresh : 0]);
|
||||||
|
gtk_widget_set_sensitive(share_button, current_id[0] && current_code[0]);
|
||||||
|
gtk_label_set_text(GTK_LABEL(hint_label), s->allowed
|
||||||
|
? "对方在 UU 远程「开始协助」中输入设备 ID 和验证码即可连接"
|
||||||
|
: "已关闭:他人暂时无法远程协助本机,打开右上角开关后才能连接");
|
||||||
|
}
|
||||||
|
g_free(s->id); g_free(s->code); g_free(s);
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// id is already formatted for display (e.g. "123 456 789").
|
||||||
|
void uu_assist_ui_set(const char *id, const char *code, int allowed, int verify, int refresh, int has_custom) {
|
||||||
|
if (!ui_started || init_state != 1) return;
|
||||||
|
State *s = g_new0(State, 1);
|
||||||
|
s->id = g_strdup(id ? id : ""); s->code = g_strdup(code ? code : ""); s->allowed = allowed;
|
||||||
|
s->verify = verify; s->refresh = refresh; s->has_custom = has_custom;
|
||||||
|
g_idle_add(apply_state, s);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pops the next user command (see CMD_*), copying its argument into arg.
|
||||||
|
int uu_assist_ui_take_command(char *arg, size_t len) {
|
||||||
|
int cmd = CMD_NONE;
|
||||||
|
if (arg && len) arg[0] = 0;
|
||||||
|
pthread_mutex_lock(&q_lock);
|
||||||
|
if (q_len) {
|
||||||
|
Cmd *c = &queue[q_head];
|
||||||
|
cmd = c->cmd; if (arg && len) g_strlcpy(arg, c->arg, len);
|
||||||
|
memset(c->arg, 0, sizeof c->arg); // drop the custom code copy promptly
|
||||||
|
q_head = (q_head + 1) % 16; q_len--;
|
||||||
|
}
|
||||||
|
pthread_mutex_unlock(&q_lock);
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
int uu_assist_ui_closed(void) { return atomic_load(&closed); }
|
||||||
|
|
||||||
|
static gboolean apply_present(gpointer data) {
|
||||||
|
(void)data;
|
||||||
|
if (window) { gtk_widget_show(window); gtk_window_present(GTK_WINDOW(window)); }
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
void uu_assist_ui_present(void) { if (ui_started && init_state == 1 && !atomic_load(&closed)) g_idle_add(apply_present, NULL); }
|
||||||
|
|
||||||
|
static gboolean apply_close(gpointer data) { (void)data; if (window) gtk_widget_destroy(window); return G_SOURCE_REMOVE; }
|
||||||
|
void uu_assist_ui_close(void) { if (ui_started && init_state == 1 && !atomic_load(&closed)) g_idle_add(apply_close, NULL); }
|
||||||
|
|
||||||
|
// ---- Account card API (QR login / signed-in user); same names as the former login window ----
|
||||||
|
typedef struct { unsigned char *rgba; int edge; } QrUpdate;
|
||||||
|
typedef struct { char *text; int kind; } StatusUpdate;
|
||||||
|
|
||||||
|
static gboolean apply_qr(gpointer data) {
|
||||||
|
QrUpdate *u = data;
|
||||||
|
if (window) {
|
||||||
|
GdkPixbuf *pb = gdk_pixbuf_new_from_data(u->rgba, GDK_COLORSPACE_RGB, TRUE, 8, u->edge, u->edge, u->edge * 4, NULL, NULL);
|
||||||
|
GdkPixbuf *scaled = gdk_pixbuf_scale_simple(pb, 180, 180, GDK_INTERP_NEAREST);
|
||||||
|
gtk_image_set_from_pixbuf(GTK_IMAGE(qr_image), scaled);
|
||||||
|
g_object_unref(scaled); g_object_unref(pb);
|
||||||
|
gtk_stack_set_visible_child_name(GTK_STACK(qr_stack), "qr");
|
||||||
|
gtk_stack_set_visible_child_name(GTK_STACK(account_stack), "qr");
|
||||||
|
}
|
||||||
|
g_free(u->rgba); g_free(u);
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
void uu_login_ui_set_qr(const unsigned char *rgba, int edge) {
|
||||||
|
if (!ui_started || init_state != 1 || atomic_load(&closed) || edge <= 0) return;
|
||||||
|
QrUpdate *u = g_new(QrUpdate, 1);
|
||||||
|
u->rgba = g_memdup2(rgba, (gsize)edge * edge * 4); u->edge = edge;
|
||||||
|
g_idle_add(apply_qr, u);
|
||||||
|
}
|
||||||
|
static gboolean apply_login_status(gpointer data) {
|
||||||
|
StatusUpdate *u = data;
|
||||||
|
if (window) {
|
||||||
|
GtkStyleContext *ctx = gtk_widget_get_style_context(login_status);
|
||||||
|
gtk_style_context_remove_class(ctx, "ok"); gtk_style_context_remove_class(ctx, "error");
|
||||||
|
if (u->kind == 1) gtk_style_context_add_class(ctx, "ok");
|
||||||
|
if (u->kind == 2) gtk_style_context_add_class(ctx, "error");
|
||||||
|
gtk_label_set_text(GTK_LABEL(login_status), u->text);
|
||||||
|
}
|
||||||
|
g_free(u->text); g_free(u);
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
void uu_login_ui_set_status(const char *text, int kind) {
|
||||||
|
if (!ui_started || init_state != 1 || atomic_load(&closed)) return;
|
||||||
|
StatusUpdate *u = g_new(StatusUpdate, 1);
|
||||||
|
u->text = g_strdup(text ? text : ""); u->kind = kind;
|
||||||
|
g_idle_add(apply_login_status, u);
|
||||||
|
}
|
||||||
|
static gboolean apply_user(gpointer data) {
|
||||||
|
if (window) {
|
||||||
|
gtk_label_set_text(GTK_LABEL(user_label), data);
|
||||||
|
gtk_stack_set_visible_child_name(GTK_STACK(account_stack), "user");
|
||||||
|
show_account = 1; apply_sections(NULL);
|
||||||
|
}
|
||||||
|
g_free(data);
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
// Signed in: show the user page with this (already masked) account name.
|
||||||
|
void uu_ui_set_user(const char *name) {
|
||||||
|
if (!ui_started || init_state != 1) return;
|
||||||
|
g_idle_add(apply_user, g_strdup(name ? name : ""));
|
||||||
|
}
|
||||||
|
void uu_login_ui_set_done(const char *text) { uu_ui_set_user(text); }
|
||||||
|
int uu_login_ui_closed(void) { return atomic_load(&closed); }
|
||||||
|
void uu_login_ui_close(void) { uu_assist_ui_close(); }
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
/* Opt-in KMS prototype: read active primary-plane linear XRGB/ARGB buffers.
|
||||||
|
* No modesetting, DRM master acquisition, authentication or content logging.
|
||||||
|
*/
|
||||||
|
#include <xf86drm.h>
|
||||||
|
#include <xf86drmMode.h>
|
||||||
|
#include <drm_fourcc.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
struct uu_kms { int fd; uint32_t crtc, plane, width, height; };
|
||||||
|
static uint32_t primary_plane(int fd, uint32_t crtc_id) {
|
||||||
|
drmModePlaneRes *planes=drmModeGetPlaneResources(fd);
|
||||||
|
if (!planes) return 0;
|
||||||
|
uint32_t selected=0;
|
||||||
|
for (uint32_t i=0;i<planes->count_planes && !selected;i++) {
|
||||||
|
drmModePlane *plane=drmModeGetPlane(fd,planes->planes[i]);
|
||||||
|
if (plane && plane->crtc_id==crtc_id && plane->fb_id) {
|
||||||
|
drmModeObjectProperties *props=drmModeObjectGetProperties(fd,plane->plane_id,DRM_MODE_OBJECT_PLANE);
|
||||||
|
if (props) {
|
||||||
|
for (uint32_t j=0;j<props->count_props;j++) {
|
||||||
|
drmModePropertyRes *prop=drmModeGetProperty(fd,props->props[j]);
|
||||||
|
if (prop && strcmp(prop->name,"type")==0 && props->prop_values[j]==DRM_PLANE_TYPE_PRIMARY) selected=plane->plane_id;
|
||||||
|
if (prop) drmModeFreeProperty(prop);
|
||||||
|
}
|
||||||
|
drmModeFreeObjectProperties(props);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (plane) drmModeFreePlane(plane);
|
||||||
|
}
|
||||||
|
drmModeFreePlaneResources(planes);
|
||||||
|
return selected;
|
||||||
|
}
|
||||||
|
static void close_handles(int fd, drmModeFB2 *fb) {
|
||||||
|
for (int i=0;i<4;i++) {
|
||||||
|
if (!fb->handles[i]) continue;
|
||||||
|
int duplicate=0;
|
||||||
|
for (int j=0;j<i;j++) if (fb->handles[j]==fb->handles[i]) duplicate=1;
|
||||||
|
if (!duplicate) { struct drm_gem_close c={.handle=fb->handles[i]}; drmIoctl(fd,DRM_IOCTL_GEM_CLOSE,&c); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void uu_kms_close(struct uu_kms *s) { if (s) { close(s->fd); free(s); } }
|
||||||
|
struct uu_kms *uu_kms_open(const char *device, uint32_t *w, uint32_t *h) {
|
||||||
|
int fd=open(device,O_RDWR|O_CLOEXEC);
|
||||||
|
if (fd<0) return NULL;
|
||||||
|
if (drmSetClientCap(fd,DRM_CLIENT_CAP_UNIVERSAL_PLANES,1)<0) { close(fd); return NULL; }
|
||||||
|
drmModeRes *res=drmModeGetResources(fd);
|
||||||
|
if (!res) { close(fd); return NULL; }
|
||||||
|
struct uu_kms *s=NULL;
|
||||||
|
for (int i=0;i<res->count_crtcs;i++) {
|
||||||
|
drmModeCrtc *crtc=drmModeGetCrtc(fd,res->crtcs[i]);
|
||||||
|
uint32_t plane=crtc ? primary_plane(fd,crtc->crtc_id) : 0;
|
||||||
|
if (crtc && plane && crtc->mode_valid && crtc->width && crtc->height && crtc->width<=16384 && crtc->height<=16384) {
|
||||||
|
s=calloc(1,sizeof(*s));
|
||||||
|
if (s) { s->fd=fd; s->crtc=crtc->crtc_id; s->plane=plane; s->width=crtc->width; s->height=crtc->height; *w=s->width; *h=s->height; }
|
||||||
|
}
|
||||||
|
if (crtc) drmModeFreeCrtc(crtc);
|
||||||
|
if (s) break;
|
||||||
|
}
|
||||||
|
drmModeFreeResources(res);
|
||||||
|
if (!s) close(fd);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
int uu_kms_read(struct uu_kms *s, uint8_t *out, size_t length) {
|
||||||
|
drmModeCrtc *crtc=drmModeGetCrtc(s->fd,s->crtc);
|
||||||
|
if (!crtc) return -1;
|
||||||
|
drmModePlane *plane=drmModeGetPlane(s->fd,s->plane);
|
||||||
|
uint32_t id=plane ? plane->fb_id : 0;
|
||||||
|
if (plane) drmModeFreePlane(plane);
|
||||||
|
int valid=crtc->mode_valid && crtc->width==s->width && crtc->height==s->height;
|
||||||
|
drmModeFreeCrtc(crtc);
|
||||||
|
if (!valid || !id) return -2; /* mode changed: caller must reopen */
|
||||||
|
drmModeFB2 *fb=drmModeGetFB2(s->fd,id);
|
||||||
|
if (!fb) return -3;
|
||||||
|
int result=-4;
|
||||||
|
if (!fb->handles[0]) { result=-5; goto done; } /* DRM master or CAP_SYS_ADMIN needed */
|
||||||
|
if (fb->width!=s->width || fb->height!=s->height ||
|
||||||
|
(fb->pixel_format!=DRM_FORMAT_XRGB8888 && fb->pixel_format!=DRM_FORMAT_ARGB8888) ||
|
||||||
|
((fb->flags & DRM_MODE_FB_MODIFIERS) && fb->modifier!=DRM_FORMAT_MOD_LINEAR) ||
|
||||||
|
fb->handles[1] || fb->pitches[0] < s->width*4 || length!=(size_t)s->width*s->height*4) goto done;
|
||||||
|
struct drm_mode_map_dumb map={.handle=fb->handles[0]};
|
||||||
|
if (drmIoctl(s->fd,DRM_IOCTL_MODE_MAP_DUMB,&map)<0) { result=-6; goto done; }
|
||||||
|
size_t bytes=(size_t)fb->pitches[0]*fb->height+fb->offsets[0];
|
||||||
|
void *data=mmap(NULL,bytes,PROT_READ,MAP_SHARED,s->fd,map.offset);
|
||||||
|
if (data==MAP_FAILED) { result=-7; goto done; }
|
||||||
|
for (uint32_t y=0;y<s->height;y++) {
|
||||||
|
uint8_t *row=out+(size_t)y*s->width*4;
|
||||||
|
memcpy(row,(uint8_t*)data+fb->offsets[0]+(size_t)y*fb->pitches[0],s->width*4);
|
||||||
|
for (uint32_t x=0;x<s->width;x++) row[x*4+3]=255;
|
||||||
|
}
|
||||||
|
munmap(data,bytes);
|
||||||
|
result=0;
|
||||||
|
done:
|
||||||
|
close_handles(s->fd,fb);
|
||||||
|
drmModeFreeFB2(fb);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
@@ -1,321 +0,0 @@
|
|||||||
// GTK3 QR login window for `uuremote-server login`. The window runs its own GTK main loop on a
|
|
||||||
// dedicated thread; the Rust login flow pushes QR images and status text through g_idle_add.
|
|
||||||
// Layout follows the official login window (QR on the left), but uses no NetEase assets: the
|
|
||||||
// mark is drawn with cairo and the right column shows this host's hardware summary instead of SMS.
|
|
||||||
#include <gtk/gtk.h>
|
|
||||||
#include <pthread.h>
|
|
||||||
#include <stdatomic.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
static const char *CSS =
|
|
||||||
"window { background-image: linear-gradient(165deg, #d6e8ff 0%, #eaf3ff 40%, #ffffff 100%); }\n"
|
|
||||||
"* { font-family: 'Noto Sans CJK SC', 'Noto Sans SC', sans-serif; }\n"
|
|
||||||
".title { font-size: 26px; font-weight: 800; color: #111111; }\n"
|
|
||||||
".subtitle { font-size: 13px; color: #6b7280; }\n"
|
|
||||||
".heading { font-size: 19px; font-weight: 700; color: #1d1d1f; }\n"
|
|
||||||
".card { background-color: #ffffff; border-radius: 10px; padding: 14px; }\n"
|
|
||||||
".caption { font-size: 14px; color: #555555; }\n"
|
|
||||||
".status { font-size: 13px; color: #2f6bff; }\n"
|
|
||||||
".status.ok { color: #16a34a; }\n"
|
|
||||||
".status.error { color: #dc2626; }\n"
|
|
||||||
".key { font-size: 13px; color: #8a8f98; }\n"
|
|
||||||
".value { font-size: 13px; color: #222222; }\n"
|
|
||||||
".note { font-size: 12px; color: #6b7280; }\n"
|
|
||||||
".done-mark { font-size: 72px; color: #16a34a; }\n"
|
|
||||||
".done-text { font-size: 17px; font-weight: 700; color: #1d1d1f; }\n"
|
|
||||||
".footer { font-size: 12px; color: #9ca3af; }\n"
|
|
||||||
"button.primary { background-image: none; background-color: #2f6bff; color: #ffffff; border: none;"
|
|
||||||
" border-radius: 6px; padding: 6px 28px; font-size: 15px; }\n"
|
|
||||||
"button.primary:hover { background-color: #1f5ae6; }\n"
|
|
||||||
"separator { background-color: #dfe5ee; min-width: 1px; }\n";
|
|
||||||
|
|
||||||
typedef struct { unsigned char *rgba; int edge; } QrUpdate;
|
|
||||||
typedef struct { char *text; int kind; } StatusUpdate;
|
|
||||||
|
|
||||||
static atomic_int closed;
|
|
||||||
static pthread_t ui_thread;
|
|
||||||
static int ui_started;
|
|
||||||
static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
||||||
static pthread_cond_t init_cond = PTHREAD_COND_INITIALIZER;
|
|
||||||
static int init_state; // 0 pending, 1 ok, -1 failed
|
|
||||||
static char *info[4];
|
|
||||||
static GtkWidget *window, *stack, *qr_image, *status_label, *done_label, *done_button;
|
|
||||||
|
|
||||||
static gboolean draw_mark(GtkWidget *w, cairo_t *cr, gpointer data) {
|
|
||||||
(void)w; (void)data;
|
|
||||||
cairo_pattern_t *g = cairo_pattern_create_linear(0, 0, 44, 44);
|
|
||||||
cairo_pattern_add_color_stop_rgb(g, 0, 0.13, 0.83, 0.93);
|
|
||||||
cairo_pattern_add_color_stop_rgb(g, 1, 0.18, 0.42, 1.0);
|
|
||||||
double r = 11, s = 44;
|
|
||||||
cairo_new_sub_path(cr);
|
|
||||||
cairo_arc(cr, s - r, r, r, -G_PI / 2, 0);
|
|
||||||
cairo_arc(cr, s - r, s - r, r, 0, G_PI / 2);
|
|
||||||
cairo_arc(cr, r, s - r, r, G_PI / 2, G_PI);
|
|
||||||
cairo_arc(cr, r, r, r, G_PI, 3 * G_PI / 2);
|
|
||||||
cairo_close_path(cr);
|
|
||||||
cairo_set_source(cr, g);
|
|
||||||
cairo_fill(cr);
|
|
||||||
cairo_pattern_destroy(g);
|
|
||||||
cairo_set_source_rgb(cr, 1, 1, 1);
|
|
||||||
cairo_select_font_face(cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
|
|
||||||
cairo_set_font_size(cr, 19);
|
|
||||||
cairo_text_extents_t e;
|
|
||||||
cairo_text_extents(cr, "UU", &e);
|
|
||||||
cairo_move_to(cr, (s - e.width) / 2 - e.x_bearing, (s - e.height) / 2 - e.y_bearing);
|
|
||||||
cairo_show_text(cr, "UU");
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
static GtkWidget *label(const char *text, const char *cls) {
|
|
||||||
GtkWidget *l = gtk_label_new(text);
|
|
||||||
gtk_style_context_add_class(gtk_widget_get_style_context(l), cls);
|
|
||||||
return l;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void info_row(GtkGrid *grid, int row, const char *key, const char *value) {
|
|
||||||
GtkWidget *k = label(key, "key"), *v = label(value && *value ? value : "-", "value");
|
|
||||||
gtk_widget_set_halign(k, GTK_ALIGN_START);
|
|
||||||
gtk_widget_set_valign(k, GTK_ALIGN_START);
|
|
||||||
gtk_widget_set_halign(v, GTK_ALIGN_START);
|
|
||||||
gtk_label_set_xalign(GTK_LABEL(v), 0);
|
|
||||||
gtk_label_set_line_wrap(GTK_LABEL(v), TRUE);
|
|
||||||
gtk_label_set_max_width_chars(GTK_LABEL(v), 26);
|
|
||||||
gtk_grid_attach(grid, k, 0, row, 1, 1);
|
|
||||||
gtk_grid_attach(grid, v, 1, row, 1, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void on_destroy(GtkWidget *w, gpointer data) {
|
|
||||||
(void)w; (void)data;
|
|
||||||
atomic_store(&closed, 1);
|
|
||||||
window = NULL;
|
|
||||||
gtk_main_quit();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void on_done_clicked(GtkButton *b, gpointer data) {
|
|
||||||
(void)b; (void)data;
|
|
||||||
if (window) gtk_widget_destroy(window);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void build_window(void) {
|
|
||||||
GtkCssProvider *css = gtk_css_provider_new();
|
|
||||||
gtk_css_provider_load_from_data(css, CSS, -1, NULL);
|
|
||||||
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css),
|
|
||||||
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
|
||||||
g_object_unref(css);
|
|
||||||
|
|
||||||
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
|
||||||
gtk_window_set_title(GTK_WINDOW(window), "UU 远程 Linux 主机 - 登录");
|
|
||||||
gtk_window_set_default_size(GTK_WINDOW(window), 720, 520);
|
|
||||||
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
|
|
||||||
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
|
|
||||||
g_signal_connect(window, "destroy", G_CALLBACK(on_destroy), NULL);
|
|
||||||
|
|
||||||
GtkWidget *root = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
|
||||||
gtk_container_set_border_width(GTK_CONTAINER(root), 24);
|
|
||||||
gtk_container_add(GTK_CONTAINER(window), root);
|
|
||||||
|
|
||||||
GtkWidget *header = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 12);
|
|
||||||
gtk_widget_set_halign(header, GTK_ALIGN_CENTER);
|
|
||||||
GtkWidget *mark = gtk_drawing_area_new();
|
|
||||||
gtk_widget_set_size_request(mark, 44, 44);
|
|
||||||
gtk_widget_set_valign(mark, GTK_ALIGN_CENTER);
|
|
||||||
g_signal_connect(mark, "draw", G_CALLBACK(draw_mark), NULL);
|
|
||||||
GtkWidget *titles = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
|
||||||
GtkWidget *title = label("UU 远程 · Linux 主机", "title");
|
|
||||||
gtk_widget_set_halign(title, GTK_ALIGN_START);
|
|
||||||
GtkWidget *subtitle = label("登录后,本机会出现在你的 UU 远程设备列表中", "subtitle");
|
|
||||||
gtk_widget_set_halign(subtitle, GTK_ALIGN_START);
|
|
||||||
gtk_box_pack_start(GTK_BOX(titles), title, FALSE, FALSE, 0);
|
|
||||||
gtk_box_pack_start(GTK_BOX(titles), subtitle, FALSE, FALSE, 0);
|
|
||||||
gtk_box_pack_start(GTK_BOX(header), mark, FALSE, FALSE, 0);
|
|
||||||
gtk_box_pack_start(GTK_BOX(header), titles, FALSE, FALSE, 0);
|
|
||||||
gtk_box_pack_start(GTK_BOX(root), header, FALSE, FALSE, 4);
|
|
||||||
|
|
||||||
GtkWidget *columns = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
|
|
||||||
gtk_box_set_homogeneous(GTK_BOX(columns), FALSE);
|
|
||||||
gtk_widget_set_margin_top(columns, 22);
|
|
||||||
gtk_box_pack_start(GTK_BOX(root), columns, TRUE, TRUE, 0);
|
|
||||||
|
|
||||||
// Left: QR login.
|
|
||||||
GtkWidget *left = gtk_box_new(GTK_ORIENTATION_VERTICAL, 12);
|
|
||||||
gtk_widget_set_size_request(left, 330, -1);
|
|
||||||
gtk_box_pack_start(GTK_BOX(left), label("扫码登录", "heading"), FALSE, FALSE, 0);
|
|
||||||
GtkWidget *card = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
|
||||||
gtk_style_context_add_class(gtk_widget_get_style_context(card), "card");
|
|
||||||
gtk_widget_set_halign(card, GTK_ALIGN_CENTER);
|
|
||||||
gtk_widget_set_size_request(card, 252, 252);
|
|
||||||
stack = gtk_stack_new();
|
|
||||||
gtk_stack_set_transition_type(GTK_STACK(stack), GTK_STACK_TRANSITION_TYPE_CROSSFADE);
|
|
||||||
GtkWidget *loading = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
|
|
||||||
gtk_widget_set_valign(loading, GTK_ALIGN_CENTER);
|
|
||||||
GtkWidget *spinner = gtk_spinner_new();
|
|
||||||
gtk_spinner_start(GTK_SPINNER(spinner));
|
|
||||||
gtk_widget_set_size_request(spinner, 32, 32);
|
|
||||||
gtk_box_pack_start(GTK_BOX(loading), spinner, FALSE, FALSE, 0);
|
|
||||||
gtk_box_pack_start(GTK_BOX(loading), label("正在获取二维码…", "caption"), FALSE, FALSE, 0);
|
|
||||||
gtk_stack_add_named(GTK_STACK(stack), loading, "loading");
|
|
||||||
qr_image = gtk_image_new();
|
|
||||||
gtk_stack_add_named(GTK_STACK(stack), qr_image, "qr");
|
|
||||||
GtkWidget *done = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4);
|
|
||||||
gtk_widget_set_valign(done, GTK_ALIGN_CENTER);
|
|
||||||
gtk_box_pack_start(GTK_BOX(done), label("✓", "done-mark"), FALSE, FALSE, 0);
|
|
||||||
done_label = label("登录成功", "done-text");
|
|
||||||
gtk_label_set_justify(GTK_LABEL(done_label), GTK_JUSTIFY_CENTER);
|
|
||||||
gtk_label_set_line_wrap(GTK_LABEL(done_label), TRUE);
|
|
||||||
gtk_box_pack_start(GTK_BOX(done), done_label, FALSE, FALSE, 0);
|
|
||||||
gtk_stack_add_named(GTK_STACK(stack), done, "done");
|
|
||||||
gtk_box_pack_start(GTK_BOX(card), stack, TRUE, TRUE, 0);
|
|
||||||
gtk_box_pack_start(GTK_BOX(left), card, FALSE, FALSE, 0);
|
|
||||||
GtkWidget *caption = label("请使用网易 UU 远程 App 扫一扫\n可快速登录", "caption");
|
|
||||||
gtk_label_set_justify(GTK_LABEL(caption), GTK_JUSTIFY_CENTER);
|
|
||||||
gtk_box_pack_start(GTK_BOX(left), caption, FALSE, FALSE, 0);
|
|
||||||
status_label = label("", "status");
|
|
||||||
gtk_label_set_line_wrap(GTK_LABEL(status_label), TRUE);
|
|
||||||
gtk_label_set_justify(GTK_LABEL(status_label), GTK_JUSTIFY_CENTER);
|
|
||||||
gtk_label_set_max_width_chars(GTK_LABEL(status_label), 34);
|
|
||||||
gtk_box_pack_start(GTK_BOX(left), status_label, FALSE, FALSE, 0);
|
|
||||||
gtk_box_pack_start(GTK_BOX(columns), left, TRUE, TRUE, 0);
|
|
||||||
|
|
||||||
GtkWidget *sep = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
|
|
||||||
gtk_widget_set_margin_top(sep, 6);
|
|
||||||
gtk_widget_set_margin_bottom(sep, 24);
|
|
||||||
gtk_box_pack_start(GTK_BOX(columns), sep, FALSE, FALSE, 0);
|
|
||||||
|
|
||||||
// Right: what will be registered.
|
|
||||||
GtkWidget *right = gtk_box_new(GTK_ORIENTATION_VERTICAL, 14);
|
|
||||||
gtk_widget_set_margin_start(right, 30);
|
|
||||||
gtk_widget_set_size_request(right, 300, -1);
|
|
||||||
GtkWidget *rh = label("本机信息", "heading");
|
|
||||||
gtk_box_pack_start(GTK_BOX(right), rh, FALSE, FALSE, 0);
|
|
||||||
GtkWidget *grid = gtk_grid_new();
|
|
||||||
gtk_grid_set_row_spacing(GTK_GRID(grid), 12);
|
|
||||||
gtk_grid_set_column_spacing(GTK_GRID(grid), 16);
|
|
||||||
gtk_widget_set_margin_top(grid, 10);
|
|
||||||
const char *keys[4] = {"设备名称", "操作系统", "处理器", "显卡"};
|
|
||||||
for (int i = 0; i < 4; i++) info_row(GTK_GRID(grid), i, keys[i], info[i]);
|
|
||||||
gtk_box_pack_start(GTK_BOX(right), grid, FALSE, FALSE, 0);
|
|
||||||
GtkWidget *note = label("登录后可在 Mac / Windows / 手机上的官方 UU 远程客户端中连接本机的桌面、文件传输和终端。"
|
|
||||||
"登录信息只保存在本机配置文件中。", "note");
|
|
||||||
gtk_label_set_line_wrap(GTK_LABEL(note), TRUE);
|
|
||||||
gtk_label_set_max_width_chars(GTK_LABEL(note), 30);
|
|
||||||
gtk_label_set_xalign(GTK_LABEL(note), 0);
|
|
||||||
gtk_widget_set_margin_top(note, 10);
|
|
||||||
gtk_box_pack_start(GTK_BOX(right), note, FALSE, FALSE, 0);
|
|
||||||
done_button = gtk_button_new_with_label("完成");
|
|
||||||
gtk_style_context_add_class(gtk_widget_get_style_context(done_button), "primary");
|
|
||||||
gtk_widget_set_halign(done_button, GTK_ALIGN_START);
|
|
||||||
gtk_widget_set_no_show_all(done_button, TRUE);
|
|
||||||
g_signal_connect(done_button, "clicked", G_CALLBACK(on_done_clicked), NULL);
|
|
||||||
gtk_box_pack_end(GTK_BOX(right), done_button, FALSE, FALSE, 20);
|
|
||||||
gtk_box_pack_start(GTK_BOX(columns), right, TRUE, TRUE, 0);
|
|
||||||
|
|
||||||
GtkWidget *footer = label("开源 Linux 主机端 · 非网易官方客户端", "footer");
|
|
||||||
gtk_box_pack_end(GTK_BOX(root), footer, FALSE, FALSE, 0);
|
|
||||||
gtk_widget_show_all(window);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void *ui_main(void *arg) {
|
|
||||||
(void)arg;
|
|
||||||
int ok = gtk_init_check(NULL, NULL);
|
|
||||||
if (ok) build_window();
|
|
||||||
pthread_mutex_lock(&init_lock);
|
|
||||||
init_state = ok ? 1 : -1;
|
|
||||||
pthread_cond_signal(&init_cond);
|
|
||||||
pthread_mutex_unlock(&init_lock);
|
|
||||||
if (ok) gtk_main();
|
|
||||||
else atomic_store(&closed, 1);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns 0 when the window is shown, -1 when no display is usable.
|
|
||||||
int uu_login_ui_start(const char *name, const char *os, const char *cpu, const char *gpu) {
|
|
||||||
if (ui_started) return init_state == 1 ? 0 : -1;
|
|
||||||
const char *src[4] = {name, os, cpu, gpu};
|
|
||||||
for (int i = 0; i < 4; i++) info[i] = g_strdup(src[i] ? src[i] : "");
|
|
||||||
if (pthread_create(&ui_thread, NULL, ui_main, NULL) != 0) return -1;
|
|
||||||
ui_started = 1;
|
|
||||||
pthread_mutex_lock(&init_lock);
|
|
||||||
while (init_state == 0) pthread_cond_wait(&init_cond, &init_lock);
|
|
||||||
pthread_mutex_unlock(&init_lock);
|
|
||||||
if (init_state != 1) { pthread_join(ui_thread, NULL); return -1; }
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static gboolean apply_qr(gpointer data) {
|
|
||||||
QrUpdate *u = data;
|
|
||||||
if (window) {
|
|
||||||
GdkPixbuf *pb = gdk_pixbuf_new_from_data(u->rgba, GDK_COLORSPACE_RGB, TRUE, 8, u->edge, u->edge, u->edge * 4,
|
|
||||||
(GdkPixbufDestroyNotify)g_free, NULL);
|
|
||||||
gtk_image_set_from_pixbuf(GTK_IMAGE(qr_image), pb);
|
|
||||||
g_object_unref(pb);
|
|
||||||
gtk_stack_set_visible_child_name(GTK_STACK(stack), "qr");
|
|
||||||
} else g_free(u->rgba);
|
|
||||||
g_free(u);
|
|
||||||
return G_SOURCE_REMOVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
void uu_login_ui_set_qr(const unsigned char *rgba, int edge) {
|
|
||||||
if (atomic_load(&closed) || edge <= 0) return;
|
|
||||||
QrUpdate *u = g_new(QrUpdate, 1);
|
|
||||||
u->rgba = g_memdup2(rgba, (gsize)edge * edge * 4);
|
|
||||||
u->edge = edge;
|
|
||||||
g_idle_add(apply_qr, u);
|
|
||||||
}
|
|
||||||
|
|
||||||
static gboolean apply_status(gpointer data) {
|
|
||||||
StatusUpdate *u = data;
|
|
||||||
if (window) {
|
|
||||||
GtkStyleContext *ctx = gtk_widget_get_style_context(status_label);
|
|
||||||
gtk_style_context_remove_class(ctx, "ok");
|
|
||||||
gtk_style_context_remove_class(ctx, "error");
|
|
||||||
if (u->kind == 1) gtk_style_context_add_class(ctx, "ok");
|
|
||||||
if (u->kind == 2) gtk_style_context_add_class(ctx, "error");
|
|
||||||
gtk_label_set_text(GTK_LABEL(status_label), u->text);
|
|
||||||
if (u->kind == 2 && !gtk_image_get_pixbuf(GTK_IMAGE(qr_image)))
|
|
||||||
gtk_widget_hide(gtk_stack_get_child_by_name(GTK_STACK(stack), "loading"));
|
|
||||||
}
|
|
||||||
g_free(u->text);
|
|
||||||
g_free(u);
|
|
||||||
return G_SOURCE_REMOVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
// kind: 0 info, 1 success, 2 error.
|
|
||||||
void uu_login_ui_set_status(const char *text, int kind) {
|
|
||||||
if (atomic_load(&closed)) return;
|
|
||||||
StatusUpdate *u = g_new(StatusUpdate, 1);
|
|
||||||
u->text = g_strdup(text ? text : "");
|
|
||||||
u->kind = kind;
|
|
||||||
g_idle_add(apply_status, u);
|
|
||||||
}
|
|
||||||
|
|
||||||
static gboolean apply_done(gpointer data) {
|
|
||||||
if (window) {
|
|
||||||
gtk_label_set_text(GTK_LABEL(done_label), data);
|
|
||||||
gtk_stack_set_visible_child_name(GTK_STACK(stack), "done");
|
|
||||||
gtk_widget_show(done_button);
|
|
||||||
gtk_widget_grab_focus(done_button);
|
|
||||||
}
|
|
||||||
g_free(data);
|
|
||||||
return G_SOURCE_REMOVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
void uu_login_ui_set_done(const char *text) {
|
|
||||||
if (atomic_load(&closed)) return;
|
|
||||||
g_idle_add(apply_done, g_strdup(text ? text : ""));
|
|
||||||
}
|
|
||||||
|
|
||||||
int uu_login_ui_closed(void) { return atomic_load(&closed); }
|
|
||||||
|
|
||||||
static gboolean apply_close(gpointer data) {
|
|
||||||
(void)data;
|
|
||||||
if (window) gtk_widget_destroy(window);
|
|
||||||
return G_SOURCE_REMOVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
void uu_login_ui_close(void) {
|
|
||||||
if (!ui_started) return;
|
|
||||||
if (!atomic_load(&closed)) g_idle_add(apply_close, NULL);
|
|
||||||
pthread_join(ui_thread, NULL);
|
|
||||||
ui_started = 0;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,589 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<protocol name="linux_dmabuf_unstable_v1">
|
||||||
|
|
||||||
|
<copyright>
|
||||||
|
Copyright © 2014, 2015 Collabora, Ltd.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
copy of this software and associated documentation files (the "Software"),
|
||||||
|
to deal in the Software without restriction, including without limitation
|
||||||
|
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
and/or sell copies of the Software, and to permit persons to whom the
|
||||||
|
Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice (including the next
|
||||||
|
paragraph) shall be included in all copies or substantial portions of the
|
||||||
|
Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||||
|
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
DEALINGS IN THE SOFTWARE.
|
||||||
|
</copyright>
|
||||||
|
|
||||||
|
<interface name="zwp_linux_dmabuf_v1" version="5">
|
||||||
|
<description summary="factory for creating dmabuf-based wl_buffers">
|
||||||
|
Following the interfaces from:
|
||||||
|
https://www.khronos.org/registry/egl/extensions/EXT/EGL_EXT_image_dma_buf_import.txt
|
||||||
|
https://www.khronos.org/registry/EGL/extensions/EXT/EGL_EXT_image_dma_buf_import_modifiers.txt
|
||||||
|
and the Linux DRM sub-system's AddFb2 ioctl.
|
||||||
|
|
||||||
|
This interface offers ways to create generic dmabuf-based wl_buffers.
|
||||||
|
|
||||||
|
Clients can use the get_surface_feedback request to get dmabuf feedback
|
||||||
|
for a particular surface. If the client wants to retrieve feedback not
|
||||||
|
tied to a surface, they can use the get_default_feedback request.
|
||||||
|
|
||||||
|
The following are required from clients:
|
||||||
|
|
||||||
|
- Clients must ensure that either all data in the dma-buf is
|
||||||
|
coherent for all subsequent read access or that coherency is
|
||||||
|
correctly handled by the underlying kernel-side dma-buf
|
||||||
|
implementation.
|
||||||
|
|
||||||
|
- Don't make any more attachments after sending the buffer to the
|
||||||
|
compositor. Making more attachments later increases the risk of
|
||||||
|
the compositor not being able to use (re-import) an existing
|
||||||
|
dmabuf-based wl_buffer.
|
||||||
|
|
||||||
|
The underlying graphics stack must ensure the following:
|
||||||
|
|
||||||
|
- The dmabuf file descriptors relayed to the server will stay valid
|
||||||
|
for the whole lifetime of the wl_buffer. This means the server may
|
||||||
|
at any time use those fds to import the dmabuf into any kernel
|
||||||
|
sub-system that might accept it.
|
||||||
|
|
||||||
|
However, when the underlying graphics stack fails to deliver the
|
||||||
|
promise, because of e.g. a device hot-unplug which raises internal
|
||||||
|
errors, after the wl_buffer has been successfully created the
|
||||||
|
compositor must not raise protocol errors to the client when dmabuf
|
||||||
|
import later fails.
|
||||||
|
|
||||||
|
To create a wl_buffer from one or more dmabufs, a client creates a
|
||||||
|
zwp_linux_dmabuf_params_v1 object with a zwp_linux_dmabuf_v1.create_params
|
||||||
|
request. All planes required by the intended format are added with
|
||||||
|
the 'add' request. Finally, a 'create' or 'create_immed' request is
|
||||||
|
issued, which has the following outcome depending on the import success.
|
||||||
|
|
||||||
|
The 'create' request,
|
||||||
|
- on success, triggers a 'created' event which provides the final
|
||||||
|
wl_buffer to the client.
|
||||||
|
- on failure, triggers a 'failed' event to convey that the server
|
||||||
|
cannot use the dmabufs received from the client.
|
||||||
|
|
||||||
|
For the 'create_immed' request,
|
||||||
|
- on success, the server immediately imports the added dmabufs to
|
||||||
|
create a wl_buffer. No event is sent from the server in this case.
|
||||||
|
- on failure, the server can choose to either:
|
||||||
|
- terminate the client by raising a fatal error.
|
||||||
|
- mark the wl_buffer as failed, and send a 'failed' event to the
|
||||||
|
client. If the client uses a failed wl_buffer as an argument to any
|
||||||
|
request, the behaviour is compositor implementation-defined.
|
||||||
|
|
||||||
|
For all DRM formats and unless specified in another protocol extension,
|
||||||
|
pre-multiplied alpha is used for pixel values.
|
||||||
|
|
||||||
|
Unless specified otherwise in another protocol extension, implicit
|
||||||
|
synchronization is used. In other words, compositors and clients must
|
||||||
|
wait and signal fences implicitly passed via the DMA-BUF's reservation
|
||||||
|
mechanism.
|
||||||
|
|
||||||
|
Disclaimer: This protocol extension has been marked stable. This copy is
|
||||||
|
no longer used and only retained for backwards compatibility. The
|
||||||
|
canonical version can be found in the stable/ directory.
|
||||||
|
</description>
|
||||||
|
|
||||||
|
<request name="destroy" type="destructor">
|
||||||
|
<description summary="unbind the factory">
|
||||||
|
Objects created through this interface, especially wl_buffers, will
|
||||||
|
remain valid.
|
||||||
|
</description>
|
||||||
|
</request>
|
||||||
|
|
||||||
|
<request name="create_params">
|
||||||
|
<description summary="create a temporary object for buffer parameters">
|
||||||
|
This temporary object is used to collect multiple dmabuf handles into
|
||||||
|
a single batch to create a wl_buffer. It can only be used once and
|
||||||
|
should be destroyed after a 'created' or 'failed' event has been
|
||||||
|
received.
|
||||||
|
</description>
|
||||||
|
<arg name="params_id" type="new_id" interface="zwp_linux_buffer_params_v1"
|
||||||
|
summary="the new temporary"/>
|
||||||
|
</request>
|
||||||
|
|
||||||
|
<event name="format">
|
||||||
|
<description summary="supported buffer format">
|
||||||
|
This event advertises one buffer format that the server supports.
|
||||||
|
All the supported formats are advertised once when the client
|
||||||
|
binds to this interface. A roundtrip after binding guarantees
|
||||||
|
that the client has received all supported formats.
|
||||||
|
|
||||||
|
For the definition of the format codes, see the
|
||||||
|
zwp_linux_buffer_params_v1::create request.
|
||||||
|
|
||||||
|
Starting version 4, the format event is deprecated and must not be
|
||||||
|
sent by compositors. Instead, use get_default_feedback or
|
||||||
|
get_surface_feedback.
|
||||||
|
</description>
|
||||||
|
<arg name="format" type="uint" summary="DRM_FORMAT code"/>
|
||||||
|
</event>
|
||||||
|
|
||||||
|
<event name="modifier" since="3">
|
||||||
|
<description summary="supported buffer format modifier">
|
||||||
|
This event advertises the formats that the server supports, along with
|
||||||
|
the modifiers supported for each format. All the supported modifiers
|
||||||
|
for all the supported formats are advertised once when the client
|
||||||
|
binds to this interface. A roundtrip after binding guarantees that
|
||||||
|
the client has received all supported format-modifier pairs.
|
||||||
|
|
||||||
|
For legacy support, DRM_FORMAT_MOD_INVALID (that is, modifier_hi ==
|
||||||
|
0x00ffffff and modifier_lo == 0xffffffff) is allowed in this event.
|
||||||
|
It indicates that the server can support the format with an implicit
|
||||||
|
modifier. When a plane has DRM_FORMAT_MOD_INVALID as its modifier, it
|
||||||
|
is as if no explicit modifier is specified. The effective modifier
|
||||||
|
will be derived from the dmabuf.
|
||||||
|
|
||||||
|
A compositor that sends valid modifiers and DRM_FORMAT_MOD_INVALID for
|
||||||
|
a given format supports both explicit modifiers and implicit modifiers.
|
||||||
|
|
||||||
|
For the definition of the format and modifier codes, see the
|
||||||
|
zwp_linux_buffer_params_v1::create and zwp_linux_buffer_params_v1::add
|
||||||
|
requests.
|
||||||
|
|
||||||
|
Starting version 4, the modifier event is deprecated and must not be
|
||||||
|
sent by compositors. Instead, use get_default_feedback or
|
||||||
|
get_surface_feedback.
|
||||||
|
</description>
|
||||||
|
<arg name="format" type="uint" summary="DRM_FORMAT code"/>
|
||||||
|
<arg name="modifier_hi" type="uint"
|
||||||
|
summary="high 32 bits of layout modifier"/>
|
||||||
|
<arg name="modifier_lo" type="uint"
|
||||||
|
summary="low 32 bits of layout modifier"/>
|
||||||
|
</event>
|
||||||
|
|
||||||
|
<!-- Version 4 additions -->
|
||||||
|
|
||||||
|
<request name="get_default_feedback" since="4">
|
||||||
|
<description summary="get default feedback">
|
||||||
|
This request creates a new wp_linux_dmabuf_feedback object not bound
|
||||||
|
to a particular surface. This object will deliver feedback about dmabuf
|
||||||
|
parameters to use if the client doesn't support per-surface feedback
|
||||||
|
(see get_surface_feedback).
|
||||||
|
</description>
|
||||||
|
<arg name="id" type="new_id" interface="zwp_linux_dmabuf_feedback_v1"/>
|
||||||
|
</request>
|
||||||
|
|
||||||
|
<request name="get_surface_feedback" since="4">
|
||||||
|
<description summary="get feedback for a surface">
|
||||||
|
This request creates a new wp_linux_dmabuf_feedback object for the
|
||||||
|
specified wl_surface. This object will deliver feedback about dmabuf
|
||||||
|
parameters to use for buffers attached to this surface.
|
||||||
|
|
||||||
|
If the surface is destroyed before the wp_linux_dmabuf_feedback object,
|
||||||
|
the feedback object becomes inert.
|
||||||
|
</description>
|
||||||
|
<arg name="id" type="new_id" interface="zwp_linux_dmabuf_feedback_v1"/>
|
||||||
|
<arg name="surface" type="object" interface="wl_surface"/>
|
||||||
|
</request>
|
||||||
|
</interface>
|
||||||
|
|
||||||
|
<interface name="zwp_linux_buffer_params_v1" version="5">
|
||||||
|
<description summary="parameters for creating a dmabuf-based wl_buffer">
|
||||||
|
This temporary object is a collection of dmabufs and other
|
||||||
|
parameters that together form a single logical buffer. The temporary
|
||||||
|
object may eventually create one wl_buffer unless cancelled by
|
||||||
|
destroying it before requesting 'create'.
|
||||||
|
|
||||||
|
Single-planar formats only require one dmabuf, however
|
||||||
|
multi-planar formats may require more than one dmabuf. For all
|
||||||
|
formats, an 'add' request must be called once per plane (even if the
|
||||||
|
underlying dmabuf fd is identical).
|
||||||
|
|
||||||
|
You must use consecutive plane indices ('plane_idx' argument for 'add')
|
||||||
|
from zero to the number of planes used by the drm_fourcc format code.
|
||||||
|
All planes required by the format must be given exactly once, but can
|
||||||
|
be given in any order. Each plane index can be set only once.
|
||||||
|
</description>
|
||||||
|
|
||||||
|
<enum name="error">
|
||||||
|
<entry name="already_used" value="0"
|
||||||
|
summary="the dmabuf_batch object has already been used to create a wl_buffer"/>
|
||||||
|
<entry name="plane_idx" value="1"
|
||||||
|
summary="plane index out of bounds"/>
|
||||||
|
<entry name="plane_set" value="2"
|
||||||
|
summary="the plane index was already set"/>
|
||||||
|
<entry name="incomplete" value="3"
|
||||||
|
summary="missing or too many planes to create a buffer"/>
|
||||||
|
<entry name="invalid_format" value="4"
|
||||||
|
summary="format not supported"/>
|
||||||
|
<entry name="invalid_dimensions" value="5"
|
||||||
|
summary="invalid width or height"/>
|
||||||
|
<entry name="out_of_bounds" value="6"
|
||||||
|
summary="offset + stride * height goes out of dmabuf bounds"/>
|
||||||
|
<entry name="invalid_wl_buffer" value="7"
|
||||||
|
summary="invalid wl_buffer resulted from importing dmabufs via
|
||||||
|
the create_immed request on given buffer_params"/>
|
||||||
|
</enum>
|
||||||
|
|
||||||
|
<request name="destroy" type="destructor">
|
||||||
|
<description summary="delete this object, used or not">
|
||||||
|
Cleans up the temporary data sent to the server for dmabuf-based
|
||||||
|
wl_buffer creation.
|
||||||
|
</description>
|
||||||
|
</request>
|
||||||
|
|
||||||
|
<request name="add">
|
||||||
|
<description summary="add a dmabuf to the temporary set">
|
||||||
|
This request adds one dmabuf to the set in this
|
||||||
|
zwp_linux_buffer_params_v1.
|
||||||
|
|
||||||
|
The 64-bit unsigned value combined from modifier_hi and modifier_lo
|
||||||
|
is the dmabuf layout modifier. DRM AddFB2 ioctl calls this the
|
||||||
|
fb modifier, which is defined in drm_mode.h of Linux UAPI.
|
||||||
|
This is an opaque token. Drivers use this token to express tiling,
|
||||||
|
compression, etc. driver-specific modifications to the base format
|
||||||
|
defined by the DRM fourcc code.
|
||||||
|
|
||||||
|
Starting from version 4, the invalid_format protocol error is sent if
|
||||||
|
the format + modifier pair was not advertised as supported.
|
||||||
|
|
||||||
|
Starting from version 5, the invalid_format protocol error is sent if
|
||||||
|
all planes don't use the same modifier.
|
||||||
|
|
||||||
|
This request raises the PLANE_IDX error if plane_idx is too large.
|
||||||
|
The error PLANE_SET is raised if attempting to set a plane that
|
||||||
|
was already set.
|
||||||
|
</description>
|
||||||
|
<arg name="fd" type="fd" summary="dmabuf fd"/>
|
||||||
|
<arg name="plane_idx" type="uint" summary="plane index"/>
|
||||||
|
<arg name="offset" type="uint" summary="offset in bytes"/>
|
||||||
|
<arg name="stride" type="uint" summary="stride in bytes"/>
|
||||||
|
<arg name="modifier_hi" type="uint"
|
||||||
|
summary="high 32 bits of layout modifier"/>
|
||||||
|
<arg name="modifier_lo" type="uint"
|
||||||
|
summary="low 32 bits of layout modifier"/>
|
||||||
|
</request>
|
||||||
|
|
||||||
|
<enum name="flags" bitfield="true">
|
||||||
|
<entry name="y_invert" value="1" summary="contents are y-inverted"/>
|
||||||
|
<entry name="interlaced" value="2" summary="content is interlaced"/>
|
||||||
|
<entry name="bottom_first" value="4" summary="bottom field first"/>
|
||||||
|
</enum>
|
||||||
|
|
||||||
|
<request name="create">
|
||||||
|
<description summary="create a wl_buffer from the given dmabufs">
|
||||||
|
This asks for creation of a wl_buffer from the added dmabuf
|
||||||
|
buffers. The wl_buffer is not created immediately but returned via
|
||||||
|
the 'created' event if the dmabuf sharing succeeds. The sharing
|
||||||
|
may fail at runtime for reasons a client cannot predict, in
|
||||||
|
which case the 'failed' event is triggered.
|
||||||
|
|
||||||
|
The 'format' argument is a DRM_FORMAT code, as defined by the
|
||||||
|
libdrm's drm_fourcc.h. The Linux kernel's DRM sub-system is the
|
||||||
|
authoritative source on how the format codes should work.
|
||||||
|
|
||||||
|
The 'flags' is a bitfield of the flags defined in enum "flags".
|
||||||
|
'y_invert' means the that the image needs to be y-flipped.
|
||||||
|
|
||||||
|
Flag 'interlaced' means that the frame in the buffer is not
|
||||||
|
progressive as usual, but interlaced. An interlaced buffer as
|
||||||
|
supported here must always contain both top and bottom fields.
|
||||||
|
The top field always begins on the first pixel row. The temporal
|
||||||
|
ordering between the two fields is top field first, unless
|
||||||
|
'bottom_first' is specified. It is undefined whether 'bottom_first'
|
||||||
|
is ignored if 'interlaced' is not set.
|
||||||
|
|
||||||
|
This protocol does not convey any information about field rate,
|
||||||
|
duration, or timing, other than the relative ordering between the
|
||||||
|
two fields in one buffer. A compositor may have to estimate the
|
||||||
|
intended field rate from the incoming buffer rate. It is undefined
|
||||||
|
whether the time of receiving wl_surface.commit with a new buffer
|
||||||
|
attached, applying the wl_surface state, wl_surface.frame callback
|
||||||
|
trigger, presentation, or any other point in the compositor cycle
|
||||||
|
is used to measure the frame or field times. There is no support
|
||||||
|
for detecting missed or late frames/fields/buffers either, and
|
||||||
|
there is no support whatsoever for cooperating with interlaced
|
||||||
|
compositor output.
|
||||||
|
|
||||||
|
The composited image quality resulting from the use of interlaced
|
||||||
|
buffers is explicitly undefined. A compositor may use elaborate
|
||||||
|
hardware features or software to deinterlace and create progressive
|
||||||
|
output frames from a sequence of interlaced input buffers, or it
|
||||||
|
may produce substandard image quality. However, compositors that
|
||||||
|
cannot guarantee reasonable image quality in all cases are recommended
|
||||||
|
to just reject all interlaced buffers.
|
||||||
|
|
||||||
|
Any argument errors, including non-positive width or height,
|
||||||
|
mismatch between the number of planes and the format, bad
|
||||||
|
format, bad offset or stride, may be indicated by fatal protocol
|
||||||
|
errors: INCOMPLETE, INVALID_FORMAT, INVALID_DIMENSIONS,
|
||||||
|
OUT_OF_BOUNDS.
|
||||||
|
|
||||||
|
Dmabuf import errors in the server that are not obvious client
|
||||||
|
bugs are returned via the 'failed' event as non-fatal. This
|
||||||
|
allows attempting dmabuf sharing and falling back in the client
|
||||||
|
if it fails.
|
||||||
|
|
||||||
|
This request can be sent only once in the object's lifetime, after
|
||||||
|
which the only legal request is destroy. This object should be
|
||||||
|
destroyed after issuing a 'create' request. Attempting to use this
|
||||||
|
object after issuing 'create' raises ALREADY_USED protocol error.
|
||||||
|
|
||||||
|
It is not mandatory to issue 'create'. If a client wants to
|
||||||
|
cancel the buffer creation, it can just destroy this object.
|
||||||
|
</description>
|
||||||
|
<arg name="width" type="int" summary="base plane width in pixels"/>
|
||||||
|
<arg name="height" type="int" summary="base plane height in pixels"/>
|
||||||
|
<arg name="format" type="uint" summary="DRM_FORMAT code"/>
|
||||||
|
<arg name="flags" type="uint" enum="flags" summary="see enum flags"/>
|
||||||
|
</request>
|
||||||
|
|
||||||
|
<event name="created">
|
||||||
|
<description summary="buffer creation succeeded">
|
||||||
|
This event indicates that the attempted buffer creation was
|
||||||
|
successful. It provides the new wl_buffer referencing the dmabuf(s).
|
||||||
|
|
||||||
|
Upon receiving this event, the client should destroy the
|
||||||
|
zwp_linux_buffer_params_v1 object.
|
||||||
|
</description>
|
||||||
|
<arg name="buffer" type="new_id" interface="wl_buffer"
|
||||||
|
summary="the newly created wl_buffer"/>
|
||||||
|
</event>
|
||||||
|
|
||||||
|
<event name="failed">
|
||||||
|
<description summary="buffer creation failed">
|
||||||
|
This event indicates that the attempted buffer creation has
|
||||||
|
failed. It usually means that one of the dmabuf constraints
|
||||||
|
has not been fulfilled.
|
||||||
|
|
||||||
|
Upon receiving this event, the client should destroy the
|
||||||
|
zwp_linux_buffer_params_v1 object.
|
||||||
|
</description>
|
||||||
|
</event>
|
||||||
|
|
||||||
|
<request name="create_immed" since="2">
|
||||||
|
<description summary="immediately create a wl_buffer from the given
|
||||||
|
dmabufs">
|
||||||
|
This asks for immediate creation of a wl_buffer by importing the
|
||||||
|
added dmabufs.
|
||||||
|
|
||||||
|
In case of import success, no event is sent from the server, and the
|
||||||
|
wl_buffer is ready to be used by the client.
|
||||||
|
|
||||||
|
Upon import failure, either of the following may happen, as seen fit
|
||||||
|
by the implementation:
|
||||||
|
- the client is terminated with one of the following fatal protocol
|
||||||
|
errors:
|
||||||
|
- INCOMPLETE, INVALID_FORMAT, INVALID_DIMENSIONS, OUT_OF_BOUNDS,
|
||||||
|
in case of argument errors such as mismatch between the number
|
||||||
|
of planes and the format, bad format, non-positive width or
|
||||||
|
height, or bad offset or stride.
|
||||||
|
- INVALID_WL_BUFFER, in case the cause for failure is unknown or
|
||||||
|
platform specific.
|
||||||
|
- the server creates an invalid wl_buffer, marks it as failed and
|
||||||
|
sends a 'failed' event to the client. The result of using this
|
||||||
|
invalid wl_buffer as an argument in any request by the client is
|
||||||
|
defined by the compositor implementation.
|
||||||
|
|
||||||
|
This takes the same arguments as a 'create' request, and obeys the
|
||||||
|
same restrictions.
|
||||||
|
</description>
|
||||||
|
<arg name="buffer_id" type="new_id" interface="wl_buffer"
|
||||||
|
summary="id for the newly created wl_buffer"/>
|
||||||
|
<arg name="width" type="int" summary="base plane width in pixels"/>
|
||||||
|
<arg name="height" type="int" summary="base plane height in pixels"/>
|
||||||
|
<arg name="format" type="uint" summary="DRM_FORMAT code"/>
|
||||||
|
<arg name="flags" type="uint" enum="flags" summary="see enum flags"/>
|
||||||
|
</request>
|
||||||
|
</interface>
|
||||||
|
|
||||||
|
<interface name="zwp_linux_dmabuf_feedback_v1" version="5">
|
||||||
|
<description summary="dmabuf feedback">
|
||||||
|
This object advertises dmabuf parameters feedback. This includes the
|
||||||
|
preferred devices and the supported formats/modifiers.
|
||||||
|
|
||||||
|
The parameters are sent once when this object is created and whenever they
|
||||||
|
change. The done event is always sent once after all parameters have been
|
||||||
|
sent. When a single parameter changes, all parameters are re-sent by the
|
||||||
|
compositor.
|
||||||
|
|
||||||
|
Compositors can re-send the parameters when the current client buffer
|
||||||
|
allocations are sub-optimal. Compositors should not re-send the
|
||||||
|
parameters if re-allocating the buffers would not result in a more optimal
|
||||||
|
configuration. In particular, compositors should avoid sending the exact
|
||||||
|
same parameters multiple times in a row.
|
||||||
|
|
||||||
|
The tranche_target_device and tranche_formats events are grouped by
|
||||||
|
tranches of preference. For each tranche, a tranche_target_device, one
|
||||||
|
tranche_flags and one or more tranche_formats events are sent, followed
|
||||||
|
by a tranche_done event finishing the list. The tranches are sent in
|
||||||
|
descending order of preference. All formats and modifiers in the same
|
||||||
|
tranche have the same preference.
|
||||||
|
|
||||||
|
To send parameters, the compositor sends one main_device event, tranches
|
||||||
|
(each consisting of one tranche_target_device event, one tranche_flags
|
||||||
|
event, tranche_formats events and then a tranche_done event), then one
|
||||||
|
done event.
|
||||||
|
</description>
|
||||||
|
|
||||||
|
<request name="destroy" type="destructor">
|
||||||
|
<description summary="destroy the feedback object">
|
||||||
|
Using this request a client can tell the server that it is not going to
|
||||||
|
use the wp_linux_dmabuf_feedback object anymore.
|
||||||
|
</description>
|
||||||
|
</request>
|
||||||
|
|
||||||
|
<event name="done">
|
||||||
|
<description summary="all feedback has been sent">
|
||||||
|
This event is sent after all parameters of a wp_linux_dmabuf_feedback
|
||||||
|
object have been sent.
|
||||||
|
|
||||||
|
This allows changes to the wp_linux_dmabuf_feedback parameters to be
|
||||||
|
seen as atomic, even if they happen via multiple events.
|
||||||
|
</description>
|
||||||
|
</event>
|
||||||
|
|
||||||
|
<event name="format_table">
|
||||||
|
<description summary="format and modifier table">
|
||||||
|
This event provides a file descriptor which can be memory-mapped to
|
||||||
|
access the format and modifier table.
|
||||||
|
|
||||||
|
The table contains a tightly packed array of consecutive format +
|
||||||
|
modifier pairs. Each pair is 16 bytes wide. It contains a format as a
|
||||||
|
32-bit unsigned integer, followed by 4 bytes of unused padding, and a
|
||||||
|
modifier as a 64-bit unsigned integer. The native endianness is used.
|
||||||
|
|
||||||
|
The client must map the file descriptor in read-only private mode.
|
||||||
|
|
||||||
|
Compositors are not allowed to mutate the table file contents once this
|
||||||
|
event has been sent. Instead, compositors must create a new, separate
|
||||||
|
table file and re-send feedback parameters. Compositors are allowed to
|
||||||
|
store duplicate format + modifier pairs in the table.
|
||||||
|
</description>
|
||||||
|
<arg name="fd" type="fd" summary="table file descriptor"/>
|
||||||
|
<arg name="size" type="uint" summary="table size, in bytes"/>
|
||||||
|
</event>
|
||||||
|
|
||||||
|
<event name="main_device">
|
||||||
|
<description summary="preferred main device">
|
||||||
|
This event advertises the main device that the server prefers to use
|
||||||
|
when direct scan-out to the target device isn't possible. The
|
||||||
|
advertised main device may be different for each
|
||||||
|
wp_linux_dmabuf_feedback object, and may change over time.
|
||||||
|
|
||||||
|
There is exactly one main device. The compositor must send at least
|
||||||
|
one preference tranche with tranche_target_device equal to main_device.
|
||||||
|
|
||||||
|
Clients need to create buffers that the main device can import and
|
||||||
|
read from, otherwise creating the dmabuf wl_buffer will fail (see the
|
||||||
|
wp_linux_buffer_params.create and create_immed requests for details).
|
||||||
|
The main device will also likely be kept active by the compositor,
|
||||||
|
so clients can use it instead of waking up another device for power
|
||||||
|
savings.
|
||||||
|
|
||||||
|
In general the device is a DRM node. The DRM node type (primary vs.
|
||||||
|
render) is unspecified. Clients must not rely on the compositor sending
|
||||||
|
a particular node type. Clients cannot check two devices for equality
|
||||||
|
by comparing the dev_t value.
|
||||||
|
|
||||||
|
If explicit modifiers are not supported and the client performs buffer
|
||||||
|
allocations on a different device than the main device, then the client
|
||||||
|
must force the buffer to have a linear layout.
|
||||||
|
</description>
|
||||||
|
<arg name="device" type="array" summary="device dev_t value"/>
|
||||||
|
</event>
|
||||||
|
|
||||||
|
<event name="tranche_done">
|
||||||
|
<description summary="a preference tranche has been sent">
|
||||||
|
This event splits tranche_target_device and tranche_formats events in
|
||||||
|
preference tranches. It is sent after a set of tranche_target_device
|
||||||
|
and tranche_formats events; it represents the end of a tranche. The
|
||||||
|
next tranche will have a lower preference.
|
||||||
|
</description>
|
||||||
|
</event>
|
||||||
|
|
||||||
|
<event name="tranche_target_device">
|
||||||
|
<description summary="target device">
|
||||||
|
This event advertises the target device that the server prefers to use
|
||||||
|
for a buffer created given this tranche. The advertised target device
|
||||||
|
may be different for each preference tranche, and may change over time.
|
||||||
|
|
||||||
|
There is exactly one target device per tranche.
|
||||||
|
|
||||||
|
The target device may be a scan-out device, for example if the
|
||||||
|
compositor prefers to directly scan-out a buffer created given this
|
||||||
|
tranche. The target device may be a rendering device, for example if
|
||||||
|
the compositor prefers to texture from said buffer.
|
||||||
|
|
||||||
|
The client can use this hint to allocate the buffer in a way that makes
|
||||||
|
it accessible from the target device, ideally directly. The buffer must
|
||||||
|
still be accessible from the main device, either through direct import
|
||||||
|
or through a potentially more expensive fallback path. If the buffer
|
||||||
|
can't be directly imported from the main device then clients must be
|
||||||
|
prepared for the compositor changing the tranche priority or making
|
||||||
|
wl_buffer creation fail (see the wp_linux_buffer_params.create and
|
||||||
|
create_immed requests for details).
|
||||||
|
|
||||||
|
If the device is a DRM node, the DRM node type (primary vs. render) is
|
||||||
|
unspecified. Clients must not rely on the compositor sending a
|
||||||
|
particular node type. Clients cannot check two devices for equality by
|
||||||
|
comparing the dev_t value.
|
||||||
|
|
||||||
|
This event is tied to a preference tranche, see the tranche_done event.
|
||||||
|
</description>
|
||||||
|
<arg name="device" type="array" summary="device dev_t value"/>
|
||||||
|
</event>
|
||||||
|
|
||||||
|
<event name="tranche_formats">
|
||||||
|
<description summary="supported buffer format modifier">
|
||||||
|
This event advertises the format + modifier combinations that the
|
||||||
|
compositor supports.
|
||||||
|
|
||||||
|
It carries an array of indices, each referring to a format + modifier
|
||||||
|
pair in the last received format table (see the format_table event).
|
||||||
|
Each index is a 16-bit unsigned integer in native endianness.
|
||||||
|
|
||||||
|
For legacy support, DRM_FORMAT_MOD_INVALID is an allowed modifier.
|
||||||
|
It indicates that the server can support the format with an implicit
|
||||||
|
modifier. When a buffer has DRM_FORMAT_MOD_INVALID as its modifier, it
|
||||||
|
is as if no explicit modifier is specified. The effective modifier
|
||||||
|
will be derived from the dmabuf.
|
||||||
|
|
||||||
|
A compositor that sends valid modifiers and DRM_FORMAT_MOD_INVALID for
|
||||||
|
a given format supports both explicit modifiers and implicit modifiers.
|
||||||
|
|
||||||
|
Compositors must not send duplicate format + modifier pairs within the
|
||||||
|
same tranche or across two different tranches with the same target
|
||||||
|
device and flags.
|
||||||
|
|
||||||
|
This event is tied to a preference tranche, see the tranche_done event.
|
||||||
|
|
||||||
|
For the definition of the format and modifier codes, see the
|
||||||
|
wp_linux_buffer_params.create request.
|
||||||
|
</description>
|
||||||
|
<arg name="indices" type="array" summary="array of 16-bit indexes"/>
|
||||||
|
</event>
|
||||||
|
|
||||||
|
<enum name="tranche_flags" bitfield="true">
|
||||||
|
<entry name="scanout" value="1" summary="direct scan-out tranche"/>
|
||||||
|
</enum>
|
||||||
|
|
||||||
|
<event name="tranche_flags">
|
||||||
|
<description summary="tranche flags">
|
||||||
|
This event sets tranche-specific flags.
|
||||||
|
|
||||||
|
The scanout flag is a hint that direct scan-out may be attempted by the
|
||||||
|
compositor on the target device if the client appropriately allocates a
|
||||||
|
buffer. How to allocate a buffer that can be scanned out on the target
|
||||||
|
device is implementation-defined.
|
||||||
|
|
||||||
|
This event is tied to a preference tranche, see the tranche_done event.
|
||||||
|
</description>
|
||||||
|
<arg name="flags" type="uint" enum="tranche_flags" summary="tranche flags"/>
|
||||||
|
</event>
|
||||||
|
</interface>
|
||||||
|
|
||||||
|
</protocol>
|
||||||
+236
-36
@@ -11,6 +11,9 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <dlfcn.h>
|
||||||
|
#include "linux-dmabuf-unstable-v1-client-protocol.h"
|
||||||
#include "wlr-screencopy-unstable-v1-client-protocol.h"
|
#include "wlr-screencopy-unstable-v1-client-protocol.h"
|
||||||
#include "wlr-virtual-pointer-unstable-v1-client-protocol.h"
|
#include "wlr-virtual-pointer-unstable-v1-client-protocol.h"
|
||||||
#include "virtual-keyboard-unstable-v1-client-protocol.h"
|
#include "virtual-keyboard-unstable-v1-client-protocol.h"
|
||||||
@@ -21,6 +24,28 @@ static struct wl_display *display;
|
|||||||
static struct wl_shm *shm;
|
static struct wl_shm *shm;
|
||||||
static struct wl_seat *seat;
|
static struct wl_seat *seat;
|
||||||
static struct zwlr_screencopy_manager_v1 *copy_manager;
|
static struct zwlr_screencopy_manager_v1 *copy_manager;
|
||||||
|
static struct zwp_linux_dmabuf_v1 *dmabuf;
|
||||||
|
static int dmabuf_requested,dmabuf_disabled,dmabuf_reported;
|
||||||
|
static unsigned dmabuf_linear_formats;
|
||||||
|
/* GBM is loaded only for explicit opt-in; default builds need no GBM headers. */
|
||||||
|
struct gbm_device;
|
||||||
|
struct gbm_bo;
|
||||||
|
static struct {
|
||||||
|
void *library;
|
||||||
|
int fd;
|
||||||
|
struct gbm_device *device;
|
||||||
|
struct gbm_device *(*create_device)(int);
|
||||||
|
void (*device_destroy)(struct gbm_device*);
|
||||||
|
struct gbm_bo *(*bo_create)(struct gbm_device*,uint32_t,uint32_t,uint32_t,uint32_t);
|
||||||
|
void (*bo_destroy)(struct gbm_bo*);
|
||||||
|
int (*bo_get_fd)(struct gbm_bo*);
|
||||||
|
int (*bo_get_plane_count)(struct gbm_bo*);
|
||||||
|
uint32_t (*bo_get_stride)(struct gbm_bo*);
|
||||||
|
uint32_t (*bo_get_offset)(struct gbm_bo*,int);
|
||||||
|
uint64_t (*bo_get_modifier)(struct gbm_bo*);
|
||||||
|
void *(*bo_map)(struct gbm_bo*,uint32_t,uint32_t,uint32_t,uint32_t,uint32_t,uint32_t*,void**);
|
||||||
|
void (*bo_unmap)(struct gbm_bo*,void*);
|
||||||
|
} gbm={.fd=-1};
|
||||||
static struct zwlr_virtual_pointer_manager_v1 *pointer_manager;
|
static struct zwlr_virtual_pointer_manager_v1 *pointer_manager;
|
||||||
static struct zwp_virtual_keyboard_manager_v1 *keyboard_manager;
|
static struct zwp_virtual_keyboard_manager_v1 *keyboard_manager;
|
||||||
static struct zwlr_virtual_pointer_v1 *pointer;
|
static struct zwlr_virtual_pointer_v1 *pointer;
|
||||||
@@ -35,11 +60,24 @@ static unsigned char held_keys[768];
|
|||||||
struct output { struct wl_output *handle; char name[128]; int transform,scale; uint32_t width,height; };
|
struct output { struct wl_output *handle; char name[128]; int transform,scale; uint32_t width,height; };
|
||||||
static struct output outputs[32];
|
static struct output outputs[32];
|
||||||
static unsigned output_count, selected;
|
static unsigned output_count, selected;
|
||||||
static uint32_t width,height,stride,format,flags;
|
static uint32_t width,height;
|
||||||
static int done,failed,initialized;
|
static int initialized;
|
||||||
static struct wl_buffer *buffer;
|
/* Independent slots let the compositor fill the next frame during delivery. */
|
||||||
static void *pixels;
|
struct frame {
|
||||||
static size_t pixel_length;
|
struct zwlr_screencopy_frame_v1 *handle;
|
||||||
|
struct wl_buffer *buffer;
|
||||||
|
void *pixels;
|
||||||
|
size_t length;
|
||||||
|
uint32_t width,height,stride,format,flags;
|
||||||
|
int done,failed,submitted;
|
||||||
|
struct zwp_linux_buffer_params_v1 *params;
|
||||||
|
struct gbm_bo *bo;
|
||||||
|
int bo_fd,dmabuf_used;
|
||||||
|
void *map_data;
|
||||||
|
uint32_t dma_format,dma_width,dma_height;
|
||||||
|
};
|
||||||
|
static struct frame frames[2];
|
||||||
|
static unsigned pending;
|
||||||
|
|
||||||
static uint32_t timestamp(void) {
|
static uint32_t timestamp(void) {
|
||||||
struct timespec t; clock_gettime(CLOCK_MONOTONIC,&t);
|
struct timespec t; clock_gettime(CLOCK_MONOTONIC,&t);
|
||||||
@@ -82,6 +120,20 @@ static void output_scale(void*d,struct wl_output*o,int32_t scale) { if(scale>0 &
|
|||||||
static void output_name(void*d,struct wl_output*o,const char*name) { snprintf(((struct output*)d)->name,128,"%s",name); }
|
static void output_name(void*d,struct wl_output*o,const char*name) { snprintf(((struct output*)d)->name,128,"%s",name); }
|
||||||
static void output_description(void*d,struct wl_output*o,const char*desc) {}
|
static void output_description(void*d,struct wl_output*o,const char*desc) {}
|
||||||
static const struct wl_output_listener output_listener={output_geometry,output_mode,output_done,output_scale,output_name,output_description};
|
static const struct wl_output_listener output_listener={output_geometry,output_mode,output_done,output_scale,output_name,output_description};
|
||||||
|
static unsigned dma_format_bit(uint32_t format) {
|
||||||
|
switch(format) {
|
||||||
|
case 0x34325258: return 1; /* DRM_FORMAT_XRGB8888 */
|
||||||
|
case 0x34325241: return 2; /* DRM_FORMAT_ARGB8888 */
|
||||||
|
case 0x34324258: return 4; /* DRM_FORMAT_XBGR8888 */
|
||||||
|
case 0x34324241: return 8; /* DRM_FORMAT_ABGR8888 */
|
||||||
|
default: return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static void dma_format(void*d,struct zwp_linux_dmabuf_v1*b,uint32_t fmt) {}
|
||||||
|
static void dma_modifier(void*d,struct zwp_linux_dmabuf_v1*b,uint32_t fmt,uint32_t hi,uint32_t lo) {
|
||||||
|
if(!hi && !lo) dmabuf_linear_formats|=dma_format_bit(fmt);
|
||||||
|
}
|
||||||
|
static const struct zwp_linux_dmabuf_v1_listener dma_listener={dma_format,dma_modifier};
|
||||||
static void global(void*d,struct wl_registry*r,uint32_t id,const char*interface,uint32_t version) {
|
static void global(void*d,struct wl_registry*r,uint32_t id,const char*interface,uint32_t version) {
|
||||||
if (!strcmp(interface,"wl_shm")) shm=wl_registry_bind(r,id,&wl_shm_interface,1);
|
if (!strcmp(interface,"wl_shm")) shm=wl_registry_bind(r,id,&wl_shm_interface,1);
|
||||||
else if (!strcmp(interface,"wl_seat") && !seat) seat=wl_registry_bind(r,id,&wl_seat_interface,1);
|
else if (!strcmp(interface,"wl_seat") && !seat) seat=wl_registry_bind(r,id,&wl_seat_interface,1);
|
||||||
@@ -90,6 +142,9 @@ static void global(void*d,struct wl_registry*r,uint32_t id,const char*interface,
|
|||||||
o->scale=1;
|
o->scale=1;
|
||||||
o->handle=wl_registry_bind(r,id,&wl_output_interface,version<4?version:4);
|
o->handle=wl_registry_bind(r,id,&wl_output_interface,version<4?version:4);
|
||||||
wl_output_add_listener(o->handle,&output_listener,o);
|
wl_output_add_listener(o->handle,&output_listener,o);
|
||||||
|
} else if (!strcmp(interface,"zwp_linux_dmabuf_v1") && dmabuf_requested && version>=3) {
|
||||||
|
dmabuf=wl_registry_bind(r,id,&zwp_linux_dmabuf_v1_interface,3);
|
||||||
|
zwp_linux_dmabuf_v1_add_listener(dmabuf,&dma_listener,NULL);
|
||||||
} else if (!strcmp(interface,"zwlr_screencopy_manager_v1")) copy_manager=wl_registry_bind(r,id,&zwlr_screencopy_manager_v1_interface,version<3?version:3);
|
} else if (!strcmp(interface,"zwlr_screencopy_manager_v1")) copy_manager=wl_registry_bind(r,id,&zwlr_screencopy_manager_v1_interface,version<3?version:3);
|
||||||
else if (!strcmp(interface,"zwlr_virtual_pointer_manager_v1") && version>=2) pointer_manager=wl_registry_bind(r,id,&zwlr_virtual_pointer_manager_v1_interface,2);
|
else if (!strcmp(interface,"zwlr_virtual_pointer_manager_v1") && version>=2) pointer_manager=wl_registry_bind(r,id,&zwlr_virtual_pointer_manager_v1_interface,2);
|
||||||
else if (!strcmp(interface,"zwp_virtual_keyboard_manager_v1")) keyboard_manager=wl_registry_bind(r,id,&zwp_virtual_keyboard_manager_v1_interface,1);
|
else if (!strcmp(interface,"zwp_virtual_keyboard_manager_v1")) keyboard_manager=wl_registry_bind(r,id,&zwp_virtual_keyboard_manager_v1_interface,1);
|
||||||
@@ -100,63 +155,160 @@ static const struct wl_registry_listener registry_listener={global,global_remove
|
|||||||
static void power_mode(void*d,struct zwlr_output_power_v1*p,uint32_t mode) {}
|
static void power_mode(void*d,struct zwlr_output_power_v1*p,uint32_t mode) {}
|
||||||
static void power_failure(void*d,struct zwlr_output_power_v1*p) { power_failed=1; }
|
static void power_failure(void*d,struct zwlr_output_power_v1*p) { power_failed=1; }
|
||||||
static const struct zwlr_output_power_v1_listener power_listener={power_mode,power_failure};
|
static const struct zwlr_output_power_v1_listener power_listener={power_mode,power_failure};
|
||||||
|
static void dma_disable(const char *reason) {
|
||||||
|
/* reason is always a fixed diagnostic tag, never driver/user text. */
|
||||||
|
if(!dmabuf_disabled) fprintf(stderr,"Wayland dma-buf unavailable; falling back to SHM (%s)\n",reason);
|
||||||
|
dmabuf_disabled=1;
|
||||||
|
}
|
||||||
|
static void dma_init(void) {
|
||||||
|
if(!dmabuf_requested) return;
|
||||||
|
if(!dmabuf) {dma_disable("protocol"); return;}
|
||||||
|
if(!dmabuf_linear_formats) {dma_disable("linear-format"); return;}
|
||||||
|
gbm.library=dlopen("libgbm.so.1",RTLD_NOW|RTLD_LOCAL);
|
||||||
|
if(!gbm.library) {dma_disable("gbm-library"); return;}
|
||||||
|
#define GBM_LOAD(field,name) do { *(void**)(&gbm.field)=dlsym(gbm.library,name); if(!gbm.field) {dma_disable("gbm-symbol"); return;} } while(0)
|
||||||
|
GBM_LOAD(create_device,"gbm_create_device"); GBM_LOAD(device_destroy,"gbm_device_destroy");
|
||||||
|
GBM_LOAD(bo_create,"gbm_bo_create"); GBM_LOAD(bo_destroy,"gbm_bo_destroy");
|
||||||
|
GBM_LOAD(bo_get_fd,"gbm_bo_get_fd"); GBM_LOAD(bo_get_plane_count,"gbm_bo_get_plane_count");
|
||||||
|
GBM_LOAD(bo_get_stride,"gbm_bo_get_stride"); GBM_LOAD(bo_get_offset,"gbm_bo_get_offset");
|
||||||
|
GBM_LOAD(bo_get_modifier,"gbm_bo_get_modifier"); GBM_LOAD(bo_map,"gbm_bo_map"); GBM_LOAD(bo_unmap,"gbm_bo_unmap");
|
||||||
|
#undef GBM_LOAD
|
||||||
|
const char *device=getenv("UU_WAYLAND_DMABUF_DEVICE");
|
||||||
|
gbm.fd=open(device && *device ? device:"/dev/dri/renderD128",O_RDWR|O_CLOEXEC);
|
||||||
|
if(gbm.fd<0 || !(gbm.device=gbm.create_device(gbm.fd))) dma_disable("render-device");
|
||||||
|
}
|
||||||
|
static void frame_clear(struct frame *frame) {
|
||||||
|
if(frame->handle) zwlr_screencopy_frame_v1_destroy(frame->handle);
|
||||||
|
if(frame->params) zwp_linux_buffer_params_v1_destroy(frame->params);
|
||||||
|
if(frame->buffer) wl_buffer_destroy(frame->buffer);
|
||||||
|
if(frame->dmabuf_used && frame->pixels) gbm.bo_unmap(frame->bo,frame->map_data);
|
||||||
|
else if(frame->pixels) munmap(frame->pixels,frame->length);
|
||||||
|
if(frame->bo) { if(frame->bo_fd>=0)close(frame->bo_fd); gbm.bo_destroy(frame->bo); }
|
||||||
|
memset(frame,0,sizeof(*frame));
|
||||||
|
}
|
||||||
|
static void frame_fail(struct frame *frame) { frame->failed=frame->done=frame->submitted=1; }
|
||||||
static void copy_buffer(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h,uint32_t s) {
|
static void copy_buffer(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h,uint32_t s) {
|
||||||
format=fmt; width=w; height=h; stride=s;
|
struct frame *frame=d;
|
||||||
|
frame->format=fmt; frame->width=w; frame->height=h; frame->stride=s;
|
||||||
if (w==0 || h==0 || w>16384 || h>16384 || s<w*4 || s>131072 || (uint64_t)s*h>256*1024*1024 ||
|
if (w==0 || h==0 || w>16384 || h>16384 || s<w*4 || s>131072 || (uint64_t)s*h>256*1024*1024 ||
|
||||||
(fmt!=WL_SHM_FORMAT_XRGB8888 && fmt!=WL_SHM_FORMAT_ARGB8888 &&
|
(fmt!=WL_SHM_FORMAT_XRGB8888 && fmt!=WL_SHM_FORMAT_ARGB8888 &&
|
||||||
fmt!=WL_SHM_FORMAT_XBGR8888 && fmt!=WL_SHM_FORMAT_ABGR8888)) { failed=done=1; return; }
|
fmt!=WL_SHM_FORMAT_XBGR8888 && fmt!=WL_SHM_FORMAT_ABGR8888)) { frame_fail(frame); return; }
|
||||||
size_t length=(size_t)s*h;
|
size_t length=(size_t)s*h;
|
||||||
if (buffer) { wl_buffer_destroy(buffer); buffer=NULL; }
|
|
||||||
if (pixels) { munmap(pixels,pixel_length); pixels=NULL; }
|
|
||||||
int fd=memfd_create("uuremote-wayland-frame",MFD_CLOEXEC);
|
int fd=memfd_create("uuremote-wayland-frame",MFD_CLOEXEC);
|
||||||
if (fd<0 || ftruncate(fd,(off_t)length)!=0) { if(fd>=0)close(fd); failed=done=1; return; }
|
if (fd<0 || ftruncate(fd,(off_t)length)!=0) { if(fd>=0)close(fd); frame_fail(frame); return; }
|
||||||
pixels=mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
|
frame->pixels=mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
|
||||||
if (pixels==MAP_FAILED) { pixels=NULL; close(fd); failed=done=1; return; }
|
if (frame->pixels==MAP_FAILED) { frame->pixels=NULL; close(fd); frame_fail(frame); return; }
|
||||||
pixel_length=length;
|
frame->length=length;
|
||||||
struct wl_shm_pool *pool=wl_shm_create_pool(shm,fd,(int32_t)length);
|
struct wl_shm_pool *pool=wl_shm_create_pool(shm,fd,(int32_t)length);
|
||||||
buffer=wl_shm_pool_create_buffer(pool,0,w,h,s,fmt);
|
frame->buffer=wl_shm_pool_create_buffer(pool,0,w,h,s,fmt);
|
||||||
wl_shm_pool_destroy(pool); close(fd);
|
wl_shm_pool_destroy(pool); close(fd);
|
||||||
/* copy is valid immediately for SHM, including frame versions <3. */
|
/* copy is valid immediately for SHM, including frame versions <3. */
|
||||||
if (zwlr_screencopy_frame_v1_get_version(f)<3) zwlr_screencopy_frame_v1_copy(f,buffer);
|
if (zwlr_screencopy_frame_v1_get_version(f)<3) {
|
||||||
|
zwlr_screencopy_frame_v1_copy(f,frame->buffer); frame->submitted=1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
static void copy_flags(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t value) { flags=value; }
|
static void copy_flags(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t value) { ((struct frame*)d)->flags=value; }
|
||||||
static void copy_ready(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t hi,uint32_t lo,uint32_t ns) { done=1; }
|
static void copy_ready(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t hi,uint32_t lo,uint32_t ns) {
|
||||||
static void copy_failed(void*d,struct zwlr_screencopy_frame_v1*f) { failed=done=1; }
|
struct frame *frame=d;
|
||||||
|
if(frame->dmabuf_used) {
|
||||||
|
/* GBM READ mapping synchronizes readback, possibly via a staging buffer. */
|
||||||
|
uint32_t stride=0;
|
||||||
|
frame->pixels=gbm.bo_map(frame->bo,0,0,frame->width,frame->height,1,&stride,&frame->map_data);
|
||||||
|
if(frame->pixels==MAP_FAILED) frame->pixels=NULL;
|
||||||
|
if(!frame->pixels || stride<frame->width*4 || stride>131072 || (uint64_t)stride*frame->height>256*1024*1024) {
|
||||||
|
dma_disable("cpu-map"); frame_fail(frame); return;
|
||||||
|
}
|
||||||
|
frame->stride=stride;
|
||||||
|
if(!dmabuf_reported) {fprintf(stderr,"Wayland dma-buf CPU mapping active (not zero-copy)\n"); dmabuf_reported=1;}
|
||||||
|
}
|
||||||
|
frame->done=1;
|
||||||
|
}
|
||||||
|
static void copy_failed(void*d,struct zwlr_screencopy_frame_v1*f) { frame_fail(d); }
|
||||||
static void copy_damage(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t x,uint32_t y,uint32_t w,uint32_t h) {}
|
static void copy_damage(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t x,uint32_t y,uint32_t w,uint32_t h) {}
|
||||||
static void copy_dmabuf(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h) {}
|
static void copy_dmabuf(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h) {
|
||||||
static void copy_buffer_done(void*d,struct zwlr_screencopy_frame_v1*f) { if(buffer && !failed) zwlr_screencopy_frame_v1_copy(f,buffer); else failed=done=1; }
|
struct frame *frame=d;
|
||||||
|
if(w && h && w<=16384 && h<=16384 && (uint64_t)w*h*4<=256*1024*1024 && dma_format_bit(fmt)) {
|
||||||
|
frame->dma_format=fmt; frame->dma_width=w; frame->dma_height=h;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static void submit_shm(struct frame *frame) {
|
||||||
|
if(frame->buffer && !frame->failed) {
|
||||||
|
zwlr_screencopy_frame_v1_copy(frame->handle,frame->buffer); frame->submitted=1;
|
||||||
|
} else frame_fail(frame);
|
||||||
|
}
|
||||||
|
static void dma_created(void*d,struct zwp_linux_buffer_params_v1*params,struct wl_buffer*buffer) {
|
||||||
|
struct frame *frame=d;
|
||||||
|
zwp_linux_buffer_params_v1_destroy(params); frame->params=NULL;
|
||||||
|
wl_buffer_destroy(frame->buffer);
|
||||||
|
munmap(frame->pixels,frame->length); frame->pixels=NULL; frame->length=0;
|
||||||
|
frame->buffer=buffer; frame->dmabuf_used=1;
|
||||||
|
frame->width=frame->dma_width; frame->height=frame->dma_height;
|
||||||
|
frame->format=frame->dma_format==0x34325258 ? WL_SHM_FORMAT_XRGB8888:
|
||||||
|
frame->dma_format==0x34325241 ? WL_SHM_FORMAT_ARGB8888:frame->dma_format;
|
||||||
|
zwlr_screencopy_frame_v1_copy(frame->handle,buffer); frame->submitted=1;
|
||||||
|
}
|
||||||
|
static void dma_failed(void*d,struct zwp_linux_buffer_params_v1*params) {
|
||||||
|
struct frame *frame=d;
|
||||||
|
zwp_linux_buffer_params_v1_destroy(params); frame->params=NULL;
|
||||||
|
dma_disable("import"); submit_shm(frame);
|
||||||
|
}
|
||||||
|
static const struct zwp_linux_buffer_params_v1_listener dma_params_listener={dma_created,dma_failed};
|
||||||
|
static int dma_start(struct frame *frame) {
|
||||||
|
if(!dmabuf_requested || dmabuf_disabled || !gbm.device || frame->failed || !frame->buffer ||
|
||||||
|
!(dmabuf_linear_formats&dma_format_bit(frame->dma_format))) return 0;
|
||||||
|
/* Single-plane linear RGB only; unsupported imports are asynchronous failures,
|
||||||
|
never create_immed protocol errors. No privilege or device probing changes. */
|
||||||
|
frame->bo=gbm.bo_create(gbm.device,frame->dma_width,frame->dma_height,frame->dma_format,(1u<<2)|(1u<<4));
|
||||||
|
frame->bo_fd=-1;
|
||||||
|
if(!frame->bo) {dma_disable("allocation"); return 0;}
|
||||||
|
uint32_t stride=gbm.bo_get_stride(frame->bo);
|
||||||
|
if(gbm.bo_get_plane_count(frame->bo)!=1 || gbm.bo_get_modifier(frame->bo)!=0 ||
|
||||||
|
stride<frame->dma_width*4 || stride>131072 || (uint64_t)stride*frame->dma_height>256*1024*1024 ||
|
||||||
|
(frame->bo_fd=gbm.bo_get_fd(frame->bo))<0) {dma_disable("layout-or-export"); return 0;}
|
||||||
|
frame->params=zwp_linux_dmabuf_v1_create_params(dmabuf);
|
||||||
|
zwp_linux_buffer_params_v1_add_listener(frame->params,&dma_params_listener,frame);
|
||||||
|
zwp_linux_buffer_params_v1_add(frame->params,frame->bo_fd,0,gbm.bo_get_offset(frame->bo,0),stride,0,0);
|
||||||
|
zwp_linux_buffer_params_v1_create(frame->params,frame->dma_width,frame->dma_height,frame->dma_format,0);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
static void copy_buffer_done(void*d,struct zwlr_screencopy_frame_v1*f) {
|
||||||
|
struct frame *frame=d;
|
||||||
|
if(!dma_start(frame)) submit_shm(frame);
|
||||||
|
}
|
||||||
static const struct zwlr_screencopy_frame_v1_listener copy_listener={copy_buffer,copy_flags,copy_ready,copy_failed,copy_damage,copy_dmabuf,copy_buffer_done};
|
static const struct zwlr_screencopy_frame_v1_listener copy_listener={copy_buffer,copy_flags,copy_ready,copy_failed,copy_damage,copy_dmabuf,copy_buffer_done};
|
||||||
static int capture(void) {
|
static void request_frame(struct frame *frame) {
|
||||||
done=failed=0; flags=0;
|
|
||||||
if(buffer) { wl_buffer_destroy(buffer); buffer=NULL; }
|
|
||||||
/* Initial milestone: video excludes cursor, viewer draws its own pointer. */
|
/* Initial milestone: video excludes cursor, viewer draws its own pointer. */
|
||||||
struct zwlr_screencopy_frame_v1 *f=zwlr_screencopy_manager_v1_capture_output(copy_manager,0,outputs[selected].handle);
|
frame->handle=zwlr_screencopy_manager_v1_capture_output(copy_manager,0,outputs[selected].handle);
|
||||||
zwlr_screencopy_frame_v1_add_listener(f,©_listener,NULL);
|
zwlr_screencopy_frame_v1_add_listener(frame->handle,©_listener,frame);
|
||||||
int result=dispatch_until(&done);
|
|
||||||
zwlr_screencopy_frame_v1_destroy(f);
|
|
||||||
return result==0 && !failed && pixels ? 0:-1;
|
|
||||||
}
|
}
|
||||||
static void reset(void) {
|
static void reset(void) {
|
||||||
if (key_state) xkb_state_unref(key_state);
|
if (key_state) xkb_state_unref(key_state);
|
||||||
if (keymap) xkb_keymap_unref(keymap);
|
if (keymap) xkb_keymap_unref(keymap);
|
||||||
if (xkb) xkb_context_unref(xkb);
|
if (xkb) xkb_context_unref(xkb);
|
||||||
if (pixels) munmap(pixels,pixel_length);
|
for(unsigned i=0;i<2;i++) frame_clear(&frames[i]);
|
||||||
|
if(gbm.device) gbm.device_destroy(gbm.device);
|
||||||
|
if(gbm.fd>=0) close(gbm.fd);
|
||||||
|
if(gbm.library) dlclose(gbm.library);
|
||||||
|
memset(&gbm,0,sizeof(gbm)); gbm.fd=-1;
|
||||||
/* Disconnect drops every proxy; failure never leaves an initialized backend. */
|
/* Disconnect drops every proxy; failure never leaves an initialized backend. */
|
||||||
if (display) wl_display_disconnect(display);
|
if (display) wl_display_disconnect(display);
|
||||||
display=NULL; shm=NULL; seat=NULL; copy_manager=NULL; pointer_manager=NULL; keyboard_manager=NULL;
|
display=NULL; shm=NULL; seat=NULL; copy_manager=NULL; pointer_manager=NULL; keyboard_manager=NULL;
|
||||||
pointer=NULL; keyboard=NULL; xkb=NULL; keymap=NULL; key_state=NULL; buffer=NULL; pixels=NULL;
|
pointer=NULL; keyboard=NULL; xkb=NULL; keymap=NULL; key_state=NULL;
|
||||||
power_manager=NULL; power=NULL; power_failed=0;
|
power_manager=NULL; power=NULL; power_failed=0;
|
||||||
pixel_length=0; output_count=0; initialized=0; memset(outputs,0,sizeof(outputs));
|
dmabuf=NULL; dmabuf_requested=dmabuf_disabled=dmabuf_reported=0; dmabuf_linear_formats=0;
|
||||||
|
pending=0; output_count=0; initialized=0; memset(outputs,0,sizeof(outputs));
|
||||||
memset(held_keys,0,sizeof(held_keys));
|
memset(held_keys,0,sizeof(held_keys));
|
||||||
}
|
}
|
||||||
int uu_wl_init(int32_t *w,int32_t *h) {
|
int uu_wl_init(int32_t *w,int32_t *h) {
|
||||||
pthread_mutex_lock(&lock);
|
pthread_mutex_lock(&lock);
|
||||||
if(initialized) { *w=width; *h=height; pthread_mutex_unlock(&lock); return 0; }
|
if(initialized) { *w=width; *h=height; pthread_mutex_unlock(&lock); return 0; }
|
||||||
int result=-1;
|
int result=-1;
|
||||||
|
const char *dma=getenv("UU_WAYLAND_DMABUF"); dmabuf_requested=dma && !strcmp(dma,"1");
|
||||||
display=wl_display_connect(NULL); if (!display) goto end;
|
display=wl_display_connect(NULL); if (!display) goto end;
|
||||||
struct wl_registry *registry=wl_display_get_registry(display);
|
struct wl_registry *registry=wl_display_get_registry(display);
|
||||||
wl_registry_add_listener(registry,®istry_listener,NULL);
|
wl_registry_add_listener(registry,®istry_listener,NULL);
|
||||||
if(sync_display()!=0 || sync_display()!=0) goto end;
|
if(sync_display()!=0 || sync_display()!=0) goto end;
|
||||||
|
dma_init();
|
||||||
if(!shm || !seat || !copy_manager || !pointer_manager || !keyboard_manager || !output_count) goto end;
|
if(!shm || !seat || !copy_manager || !pointer_manager || !keyboard_manager || !output_count) goto end;
|
||||||
const char *name=getenv("UU_WAYLAND_OUTPUT"); selected=0;
|
const char *name=getenv("UU_WAYLAND_OUTPUT"); selected=0;
|
||||||
if(name && *name) {
|
if(name && *name) {
|
||||||
@@ -203,18 +355,41 @@ int uu_wl_wake(void) {
|
|||||||
}
|
}
|
||||||
int uu_wl_capture(uint8_t *out,size_t length) {
|
int uu_wl_capture(uint8_t *out,size_t length) {
|
||||||
pthread_mutex_lock(&lock);
|
pthread_mutex_lock(&lock);
|
||||||
int result=-1;
|
int result=-1,retried=0;
|
||||||
if(initialized && capture()==0 && length==(size_t)width*height*4) {
|
retry:;
|
||||||
|
struct frame *frame=&frames[pending];
|
||||||
|
if(!initialized || length!=(size_t)width*height*4) goto end;
|
||||||
|
if(!frame->handle) request_frame(frame);
|
||||||
|
if(dispatch_until(&frame->done)!=0) goto end;
|
||||||
|
if(frame->failed && frame->dmabuf_used && !retried) {
|
||||||
|
/* Mapping/copy failure retries once on SHM; connection errors do not. */
|
||||||
|
dma_disable("copy"); retried=1;
|
||||||
|
for(unsigned i=0;i<2;i++) frame_clear(&frames[i]);
|
||||||
|
pending=0; goto retry;
|
||||||
|
}
|
||||||
|
if(frame->failed || !frame->pixels || frame->width!=width || frame->height!=height) goto end;
|
||||||
|
zwlr_screencopy_frame_v1_destroy(frame->handle); frame->handle=NULL;
|
||||||
|
const char *pipeline=getenv("UU_WAYLAND_PIPELINE");
|
||||||
|
if(!pipeline || strcmp(pipeline,"0")) {
|
||||||
|
struct frame *next=&frames[1-pending];
|
||||||
|
request_frame(next);
|
||||||
|
/* Submit the next GPU copy before copying/delivering these CPU pixels. */
|
||||||
|
if(dispatch_until(&next->submitted)!=0 || wl_display_flush(display)<0) goto end;
|
||||||
|
}
|
||||||
|
{
|
||||||
for(uint32_t y=0;y<height;y++) {
|
for(uint32_t y=0;y<height;y++) {
|
||||||
uint32_t source=(flags&ZWLR_SCREENCOPY_FRAME_V1_FLAGS_Y_INVERT)?height-y-1:y;
|
uint32_t source=(frame->flags&ZWLR_SCREENCOPY_FRAME_V1_FLAGS_Y_INVERT)?height-y-1:y;
|
||||||
uint8_t *row=out+(size_t)y*width*4;
|
uint8_t *row=out+(size_t)y*width*4;
|
||||||
memcpy(row,(uint8_t*)pixels+(size_t)source*stride,width*4);
|
memcpy(row,(uint8_t*)frame->pixels+(size_t)source*frame->stride,width*4);
|
||||||
if(format==WL_SHM_FORMAT_XBGR8888 || format==WL_SHM_FORMAT_ABGR8888) {
|
if(frame->format==WL_SHM_FORMAT_XBGR8888 || frame->format==WL_SHM_FORMAT_ABGR8888) {
|
||||||
for(uint32_t x=0;x<width;x++) { uint8_t red=row[x*4]; row[x*4]=row[x*4+2]; row[x*4+2]=red; }
|
for(uint32_t x=0;x<width;x++) { uint8_t red=row[x*4]; row[x*4]=row[x*4+2]; row[x*4+2]=red; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result=0;
|
result=0;
|
||||||
}
|
}
|
||||||
|
frame_clear(frame); pending=1-pending;
|
||||||
|
end:
|
||||||
|
if(result!=0) for(unsigned i=0;i<2;i++) frame_clear(&frames[i]);
|
||||||
pthread_mutex_unlock(&lock); return result;
|
pthread_mutex_unlock(&lock); return result;
|
||||||
}
|
}
|
||||||
int uu_wl_move(int32_t x,int32_t y,int relative) {
|
int uu_wl_move(int32_t x,int32_t y,int relative) {
|
||||||
@@ -275,3 +450,28 @@ int uu_wl_key(uint64_t symbol,int pressed) {
|
|||||||
}
|
}
|
||||||
pthread_mutex_unlock(&lock); return result;
|
pthread_mutex_unlock(&lock); return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int uu_wl_release_all(void) {
|
||||||
|
pthread_mutex_lock(&lock);
|
||||||
|
int result=-1;
|
||||||
|
if(initialized) {
|
||||||
|
/* Sweep this client's virtual keyboard directly: a keysym lookup can
|
||||||
|
fail for unmapped OEM/keypad symbols before reaching held modifiers. */
|
||||||
|
for(xkb_keycode_t code=xkb_keymap_min_keycode(keymap);code<=xkb_keymap_max_keycode(keymap);code++) {
|
||||||
|
if(code<8 || code>=sizeof(held_keys) || !xkb_keymap_key_get_name(keymap,code)) continue;
|
||||||
|
zwp_virtual_keyboard_v1_key(keyboard,timestamp(),code-8,WL_KEYBOARD_KEY_STATE_RELEASED);
|
||||||
|
if(held_keys[code]) xkb_state_update_key(key_state,code,XKB_KEY_UP);
|
||||||
|
}
|
||||||
|
memset(held_keys,0,sizeof(held_keys));
|
||||||
|
zwp_virtual_keyboard_v1_modifiers(keyboard,
|
||||||
|
xkb_state_serialize_mods(key_state,XKB_STATE_MODS_DEPRESSED),
|
||||||
|
xkb_state_serialize_mods(key_state,XKB_STATE_MODS_LATCHED),
|
||||||
|
xkb_state_serialize_mods(key_state,XKB_STATE_MODS_LOCKED),
|
||||||
|
xkb_state_serialize_layout(key_state,XKB_STATE_LAYOUT_EFFECTIVE));
|
||||||
|
for(unsigned code=0x110;code<=0x114;code++)
|
||||||
|
zwlr_virtual_pointer_v1_button(pointer,timestamp(),code,WL_POINTER_BUTTON_STATE_RELEASED);
|
||||||
|
zwlr_virtual_pointer_v1_frame(pointer);
|
||||||
|
result=wl_display_flush(display)<0?-1:0;
|
||||||
|
}
|
||||||
|
pthread_mutex_unlock(&lock);return result;
|
||||||
|
}
|
||||||
+71
-2
@@ -9,6 +9,8 @@
|
|||||||
#include <sys/ipc.h>
|
#include <sys/ipc.h>
|
||||||
#include <X11/Xlib.h>
|
#include <X11/Xlib.h>
|
||||||
#include <X11/Xutil.h>
|
#include <X11/Xutil.h>
|
||||||
|
#include <X11/Xatom.h>
|
||||||
|
#include <strings.h>
|
||||||
#include <X11/extensions/XShm.h>
|
#include <X11/extensions/XShm.h>
|
||||||
#include <X11/extensions/XTest.h>
|
#include <X11/extensions/XTest.h>
|
||||||
#include <X11/extensions/Xfixes.h>
|
#include <X11/extensions/Xfixes.h>
|
||||||
@@ -58,9 +60,12 @@ int x11_init(const char *display_name) {
|
|||||||
g_display = XOpenDisplay(display_name);
|
g_display = XOpenDisplay(display_name);
|
||||||
} else {
|
} else {
|
||||||
const char *configured = getenv("DISPLAY");
|
const char *configured = getenv("DISPLAY");
|
||||||
|
/* An explicit DISPLAY that fails must not silently fall back to another (physical) screen. */
|
||||||
if (configured && *configured) g_display = XOpenDisplay(configured);
|
if (configured && *configured) g_display = XOpenDisplay(configured);
|
||||||
if (!g_display) g_display = XOpenDisplay(":0");
|
else {
|
||||||
if (!g_display) g_display = XOpenDisplay(":1");
|
g_display = XOpenDisplay(":0");
|
||||||
|
if (!g_display) g_display = XOpenDisplay(":1");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!g_display) {
|
if (!g_display) {
|
||||||
return -1;
|
return -1;
|
||||||
@@ -190,6 +195,70 @@ int x11_key_event(int keycode, int is_press) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Session boundaries must also recover XTEST state left by a previous process.
|
||||||
|
* Only query/release on this authenticated display; never log key state. */
|
||||||
|
int x11_release_all(void) {
|
||||||
|
if (!g_display) return -1;
|
||||||
|
XSync(g_display, False);
|
||||||
|
char keys[32];
|
||||||
|
XQueryKeymap(g_display, keys);
|
||||||
|
for (int code = 8; code < 256; code++) {
|
||||||
|
if (((unsigned char)keys[code >> 3] | g_injected_down[code >> 3]) & (1u << (code & 7)))
|
||||||
|
XTestFakeKeyEvent(g_display, code, False, CurrentTime);
|
||||||
|
}
|
||||||
|
/* Includes side buttons; wheel buttons are stateless and need no release. */
|
||||||
|
unsigned char mapping[256];
|
||||||
|
int buttons = XGetPointerMapping(g_display, mapping, sizeof(mapping));
|
||||||
|
for (int button = 1; button <= buttons && button <= 9; button++) {
|
||||||
|
if (button <= 3 || button >= 8) XTestFakeButtonEvent(g_display, button, False, CurrentTime);
|
||||||
|
}
|
||||||
|
if (g_restore_repeat) XAutoRepeatOn(g_display);
|
||||||
|
memset(g_injected_down, 0, sizeof(g_injected_down));
|
||||||
|
g_injected_count = g_restore_repeat = 0;
|
||||||
|
XSync(g_display, False);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---- Terminal-aware paste (Mac viewer Cmd+V arrives as Ctrl+V) ------------------------------
|
||||||
|
* Linux terminals paste with Ctrl+Shift+V; Ctrl+V quotes the next key (a paste then shows as
|
||||||
|
* "^[[200~..."). Reports whether the focused window is a terminal emulator. Uses its own
|
||||||
|
* connection so it never races the capture/input connection. */
|
||||||
|
static Display *g_probe = NULL;
|
||||||
|
|
||||||
|
static int class_is_terminal(const char *value) {
|
||||||
|
static const char *names[] = { "gnome-terminal", "gnome-terminal-server", "org.gnome.Console", "kgx", "konsole",
|
||||||
|
"xfce4-terminal", "mate-terminal", "lxterminal", "qterminal", "terminator", "tilix", "xterm", "uxterm",
|
||||||
|
"urxvt", "rxvt", "alacritty", "kitty", "wezterm", "wezterm-gui", "foot", "st-256color", "terminology",
|
||||||
|
"guake", "tilda", "deepin-terminal", "ptyxis", "io.elementary.terminal", "cool-retro-term", NULL };
|
||||||
|
if (!value) return 0;
|
||||||
|
for (int i = 0; names[i]; i++) if (strcasecmp(value, names[i]) == 0) return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Returns 1 when the focused window belongs to a terminal emulator, else 0. */
|
||||||
|
int x11_terminal_shortcut_state(void) {
|
||||||
|
if (!g_probe && !(g_probe = XOpenDisplay(NULL))) return 0;
|
||||||
|
Window focus = 0; int revert = 0;
|
||||||
|
XGetInputFocus(g_probe, &focus, &revert);
|
||||||
|
if (focus == None || focus == PointerRoot) return 0;
|
||||||
|
Window cur = focus;
|
||||||
|
for (int depth = 0; depth < 12 && cur; depth++) {
|
||||||
|
XClassHint hint = {0};
|
||||||
|
if (XGetClassHint(g_probe, cur, &hint)) {
|
||||||
|
int terminal = class_is_terminal(hint.res_name) || class_is_terminal(hint.res_class);
|
||||||
|
if (hint.res_name) XFree(hint.res_name);
|
||||||
|
if (hint.res_class) XFree(hint.res_class);
|
||||||
|
return terminal;
|
||||||
|
}
|
||||||
|
Window root, parent, *children = NULL; unsigned int n = 0;
|
||||||
|
if (!XQueryTree(g_probe, cur, &root, &parent, &children, &n)) return 0;
|
||||||
|
if (children) XFree(children);
|
||||||
|
if (parent == root) return 0;
|
||||||
|
cur = parent;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void x11_close() {
|
void x11_close() {
|
||||||
if (!g_display) return;
|
if (!g_display) return;
|
||||||
if (g_use_shm) {
|
if (g_use_shm) {
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
[Desktop Entry]
|
||||||
|
Type=Application
|
||||||
|
Name=UU 远程 Linux 主机
|
||||||
|
Name[en]=UU Remote Linux Host
|
||||||
|
GenericName=Remote assistance host
|
||||||
|
Comment=非官方 UU 远程被控端:远程协助与扫码登录
|
||||||
|
Comment[en]=Unofficial NetEase UU Remote host: remote assistance and QR sign-in
|
||||||
|
Exec=uuremote-server show
|
||||||
|
Icon=uu-remote-linux
|
||||||
|
Terminal=false
|
||||||
|
Categories=Network;RemoteAccess;
|
||||||
|
Keywords=remote;assist;UU;远程;协助;
|
||||||
|
StartupNotify=false
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" width="128" height="128" viewBox="0 0 128 128">
|
||||||
|
<!-- uu-remote-linux app icon (original artwork, GPL-3.0 with the project): a desktop being reached remotely. -->
|
||||||
|
<defs>
|
||||||
|
<linearGradient id="bg" x1="0" y1="0" x2="1" y2="1">
|
||||||
|
<stop offset="0" stop-color="#22d3ee"/>
|
||||||
|
<stop offset="1" stop-color="#2563eb"/>
|
||||||
|
</linearGradient>
|
||||||
|
</defs>
|
||||||
|
<rect x="4" y="4" width="120" height="120" rx="28" fill="url(#bg)"/>
|
||||||
|
<rect x="26" y="32" width="76" height="52" rx="7" fill="none" stroke="#ffffff" stroke-width="7"/>
|
||||||
|
<path d="M52 98h24M64 84v14" stroke="#ffffff" stroke-width="7" stroke-linecap="round"/>
|
||||||
|
<path d="M46 70l22-22M54 48h14v14" fill="none" stroke="#ffffff" stroke-width="7" stroke-linecap="round" stroke-linejoin="round"/>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 794 B |
@@ -0,0 +1,23 @@
|
|||||||
|
# Virtual user-bus desktop acceptance
|
||||||
|
|
||||||
|
Install Xvfb, xauth, xdpyinfo, Xfce and dbus-user-session (Debian) or dbus-broker (Arch).
|
||||||
|
The default is `UU_VIRTUAL_BUS=user`; use it on a dedicated headless account with no real
|
||||||
|
GUI session. Its D-Bus and systemd activation environment is shared by all that user's apps.
|
||||||
|
`private` isolates ordinary D-Bus activation but cannot redirect systemd-activated programs.
|
||||||
|
|
||||||
|
1. Install the script in `~/.local/bin`, the unit in `~/.config/systemd/user`, and
|
||||||
|
`virtual-display.conf` in the host service's `.service.d/` directory.
|
||||||
|
2. `sudo loginctl enable-linger "$USER"`; set `UU_VIRTUAL_SESSION=startxfce4` in the desktop
|
||||||
|
unit. `systemctl --user daemon-reload`; enable both units. A different display needs a
|
||||||
|
matching `DISPLAY` and `XAUTHORITY=%t/uu-virtual-N.Xauthority` in the host drop-in.
|
||||||
|
3. Reboot a disposable VM with graphical login disabled. Before logging into a desktop,
|
||||||
|
check both user units are active, Xvfb is `:99`, no real GUI login session exists, and
|
||||||
|
host availability is `ready`. The desktop unit uses notify readiness after authenticated
|
||||||
|
X11 access succeeds, so the host does not race the X socket.
|
||||||
|
4. `DISPLAY=:99 XAUTHORITY="$XDG_RUNTIME_DIR/uu-virtual-99.Xauthority" xdpyinfo` must work.
|
||||||
|
`gnome-terminal` launched over the user bus must open on `:99`. Check its window via
|
||||||
|
`xdotool search --class '[Gg]nome.[Tt]erminal|gnome-terminal-server'` (window
|
||||||
|
classes vary by GNOME Terminal version; the server process name is not always
|
||||||
|
its window class). Do not log window titles or terminal text.
|
||||||
|
5. Restart/stop the desktop unit: its Xvfb, desktop children and authority file must be
|
||||||
|
removed. A stale pre-existing display is rejected. The TCP X11 listener is disabled.
|
||||||
Executable
+89
@@ -0,0 +1,89 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Virtual desktop for a monitorless Linux host: Xvfb + a desktop session on the user bus (or an isolated private bus).
|
||||||
|
# uuremote-server then serves it with DISPLAY=$UU_VIRTUAL_DISPLAY (see README in this folder).
|
||||||
|
# UU_VIRTUAL_DISPLAY X display number (default :99)
|
||||||
|
# UU_VIRTUAL_SIZE screen size (default 1920x1080)
|
||||||
|
# UU_VIRTUAL_SESSION session command (default: first of startxfce4, mate-session, startlxqt, openbox-session, xterm)
|
||||||
|
# UU_VIRTUAL_BUS user (default): the systemd user bus (`loginctl enable-linger`) gets DISPLAY, so
|
||||||
|
# D-Bus/systemd-activated apps (GNOME terminal, portals, ...) open on the virtual screen;
|
||||||
|
# private: own dbus-run-session bus, for machines that also run a real desktop session
|
||||||
|
set -eu
|
||||||
|
display="${UU_VIRTUAL_DISPLAY:-:99}"
|
||||||
|
size="${UU_VIRTUAL_SIZE:-1920x1080}"
|
||||||
|
session="${UU_VIRTUAL_SESSION:-}"
|
||||||
|
if [ -z "$session" ]; then
|
||||||
|
for candidate in startxfce4 mate-session startlxqt openbox-session xterm; do
|
||||||
|
if command -v "$candidate" >/dev/null 2>&1; then session="$candidate"; break; fi
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
[ -n "$session" ] || { echo "no desktop session found; set UU_VIRTUAL_SESSION" >&2; exit 1; }
|
||||||
|
command -v Xvfb >/dev/null || { echo "Xvfb not installed (apt install xvfb)" >&2; exit 1; }
|
||||||
|
bus="${UU_VIRTUAL_BUS:-user}"
|
||||||
|
case "$bus" in user|private) ;; *) echo "UU_VIRTUAL_BUS must be user or private" >&2; exit 1 ;; esac
|
||||||
|
command -v xauth >/dev/null || { echo "xauth not installed" >&2; exit 1; }
|
||||||
|
command -v xdpyinfo >/dev/null || { echo "xdpyinfo not installed" >&2; exit 1; }
|
||||||
|
case "$display" in :[0-9]*) ;; *) echo "UU_VIRTUAL_DISPLAY must be a local display number" >&2; exit 1 ;; esac
|
||||||
|
number="${display#:}"
|
||||||
|
case "$number" in *[!0-9]*|'') echo "invalid display number" >&2; exit 1 ;; esac
|
||||||
|
socket="/tmp/.X11-unix/X$number"
|
||||||
|
# A live server owns the display; a dead one's leftovers (killed Xvfb) are cleaned up by Xvfb itself.
|
||||||
|
lock="/tmp/.X$number-lock"
|
||||||
|
if [ -e "$socket" ] || [ -e "$lock" ]; then
|
||||||
|
owner=$(tr -d ' \n' < "$lock" 2>/dev/null || :)
|
||||||
|
case "$owner" in ''|*[!0-9]*) owner= ;; esac
|
||||||
|
if [ -z "$owner" ] || kill -0 "$owner" 2>/dev/null; then echo "display already in use" >&2; exit 1; fi
|
||||||
|
echo "removing stale display $display left by dead process $owner" >&2
|
||||||
|
rm -f "$lock" "$socket" 2>/dev/null || { echo "cannot remove stale display files" >&2; exit 1; }
|
||||||
|
fi
|
||||||
|
umask 077
|
||||||
|
export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
|
||||||
|
authority="$XDG_RUNTIME_DIR/uu-virtual-$number.Xauthority"
|
||||||
|
# The display socket was checked above; replace a stale authority after a service crash.
|
||||||
|
[ ! -L "$authority" ] || { echo "refusing a symlink authority" >&2; exit 1; }
|
||||||
|
temporary=$(mktemp "$XDG_RUNTIME_DIR/uu-virtual-xauth.XXXXXX")
|
||||||
|
xvfb=
|
||||||
|
child=
|
||||||
|
cleanup() {
|
||||||
|
trap - EXIT INT TERM
|
||||||
|
[ -z "$child" ] || kill "$child" 2>/dev/null || :
|
||||||
|
[ -z "$xvfb" ] || kill "$xvfb" 2>/dev/null || :
|
||||||
|
[ -z "$xvfb" ] || wait "$xvfb" 2>/dev/null || :
|
||||||
|
rm -f "$authority" "$temporary"
|
||||||
|
}
|
||||||
|
trap cleanup EXIT
|
||||||
|
trap 'exit 143' TERM
|
||||||
|
trap 'exit 130' INT
|
||||||
|
cookie=$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')
|
||||||
|
xauth -f "$temporary" add "$display" MIT-MAGIC-COOKIE-1 "$cookie"
|
||||||
|
unset cookie
|
||||||
|
mv "$temporary" "$authority"
|
||||||
|
export XAUTHORITY="$authority"
|
||||||
|
|
||||||
|
Xvfb "$display" -screen 0 "${size}x24" -auth "$authority" -nolisten tcp -noreset &
|
||||||
|
xvfb=$!
|
||||||
|
export DISPLAY="$display"
|
||||||
|
unset WAYLAND_DISPLAY SESSION_MANAGER
|
||||||
|
export XDG_SESSION_TYPE=x11
|
||||||
|
ready=false
|
||||||
|
for _ in $(seq 50); do
|
||||||
|
kill -0 "$xvfb" 2>/dev/null || break
|
||||||
|
if xdpyinfo >/dev/null 2>&1; then ready=true; break; fi
|
||||||
|
sleep 0.1
|
||||||
|
done
|
||||||
|
[ "$ready" = true ] || { echo "Xvfb did not become ready" >&2; exit 1; }
|
||||||
|
if [ "$bus" = user ]; then
|
||||||
|
# Linger creates the user manager and its bus without a graphical login.
|
||||||
|
: "${XDG_RUNTIME_DIR:=/run/user/$(id -u)}"
|
||||||
|
export XDG_RUNTIME_DIR
|
||||||
|
export DBUS_SESSION_BUS_ADDRESS="unix:path=$XDG_RUNTIME_DIR/bus"
|
||||||
|
[ -S "$XDG_RUNTIME_DIR/bus" ] || { echo "user bus unavailable; enable loginctl enable-linger" >&2; exit 1; }
|
||||||
|
systemctl --user unset-environment WAYLAND_DISPLAY
|
||||||
|
dbus-update-activation-environment --systemd DISPLAY XAUTHORITY XDG_SESSION_TYPE
|
||||||
|
# Keep the supervising shell alive: exec would discard the Xvfb cleanup trap.
|
||||||
|
sh -c "$session" &
|
||||||
|
else
|
||||||
|
dbus-run-session -- sh -c "$session" &
|
||||||
|
fi
|
||||||
|
child=$!
|
||||||
|
if [ -n "${NOTIFY_SOCKET:-}" ]; then systemd-notify --ready; fi
|
||||||
|
wait "$child"
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Virtual X desktop for uu-remote-linux (Xvfb + session bus)
|
||||||
|
After=dbus.socket
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=notify
|
||||||
|
NotifyAccess=all
|
||||||
|
TimeoutStartSec=30
|
||||||
|
TimeoutStopSec=10
|
||||||
|
KillMode=control-group
|
||||||
|
# Environment=UU_VIRTUAL_SESSION=startxfce4
|
||||||
|
# Environment=UU_VIRTUAL_SIZE=1920x1080
|
||||||
|
# Environment=UU_VIRTUAL_BUS=private
|
||||||
|
ExecStart=%h/.local/bin/uu-virtual-desktop
|
||||||
|
Restart=always
|
||||||
|
RestartSec=5
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=default.target
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
# ~/.config/systemd/user/uuremote-native.service.d/virtual-display.conf
|
||||||
|
# Serve the virtual desktop instead of a physical display.
|
||||||
|
[Unit]
|
||||||
|
Requires=uu-virtual-desktop.service
|
||||||
|
After=uu-virtual-desktop.service
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Environment=DISPLAY=:99
|
||||||
|
Environment=UU_DESKTOP_BACKEND=x11
|
||||||
|
Environment=XAUTHORITY=%t/uu-virtual-99.Xauthority
|
||||||
|
UnsetEnvironment=WAYLAND_DISPLAY
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 84 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 51 KiB |
@@ -0,0 +1,18 @@
|
|||||||
|
修复 iOS 和 Windows 客户端无法打开终端。/ Fixes the terminal for iOS and Windows viewers.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- **iOS 和 Windows 客户端打开终端失败**(#12,感谢 @cnetst):iOS 提示「创建失败」,Windows 提示「连接已断开」。
|
||||||
|
终端协议的应答和官方实现不一致:会话列表改为直接返回数组并保留请求编号;客户端指定会话编号时沿用该编号;
|
||||||
|
「打开成功」的应答保证先于终端输出发送。
|
||||||
|
✅ 实机验收:官方 iOS 和 Mac 客户端都能正常创建终端、执行命令,后连接的客户端能看到并接着使用已有的终端会话。
|
||||||
|
Terminal replies now match the official host (bare-array session list, client session ids, OPEN_RESP first).
|
||||||
|
Verified with the official iOS and Mac viewers.
|
||||||
|
|
||||||
|
### 说明 / Note
|
||||||
|
- 同一时间只能有一台控制设备连接同一个被控端:另一台设备连上时,先连接的会被云端断开,这是网易 UU 的服务端行为,与官方被控端一致。
|
||||||
|
The UU cloud allows one controlling device at a time; a new controller disconnects the previous one (official behavior).
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.10-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
远程协助(实验性)、统一主窗口和应用图标。/ Remote assistance (experimental), a unified main window and an app icon.
|
||||||
|
|
||||||
|
<p align="center"><img src="https://raw.githubusercontent.com/VallierDev/uu-remote-linux/v0.1.11/docs/images/main-window.png" width="560" alt="main window"></p>
|
||||||
|
|
||||||
|
### 新增 / Added
|
||||||
|
- **远程协助(实验性)**:和官方客户端一样,把「设备 ID + 验证码」发给别人,对方在 UU 远程的「开始协助」里输入即可连接本机。
|
||||||
|
- **未登录也能用**(访客身份);**登录后同样可用**,设备 ID 不变。
|
||||||
|
- 「允许他人远程协助」开关(默认关闭);验证方式:仅临时验证码 / 仅自定义验证码(8–16 位字母+数字)/ 两种都可以;
|
||||||
|
临时验证码刷新:每次远控结束 / 每小时 / 每 12 小时 / 每 24 小时 / 不刷新;ID 和验证码各有「复制」按钮,另有「复制并分享」。
|
||||||
|
- 协议参照官方 Windows 精简版被控端实现。✅ 用官方 Mac 客户端实测:访客连接、错误验证码被拒绝、用完自动换码、关闭后不可连接、
|
||||||
|
登录与退出登录的切换。⚠️ **登录状态下用另一个账号发起远程协助尚未实测**(同账号设备之间不能互相协助,这是官方限制)。
|
||||||
|
- 安全:拿到 ID 和验证码的人可以完全控制这台电脑,只发给信任的人;验证码只显示在本机窗口,不写入日志。
|
||||||
|
Remote assistance like the official client (device ID + code), before and after sign-in. Verified with the official
|
||||||
|
Mac viewer except signed-in assistance from another account.
|
||||||
|
- **统一主窗口**:一个窗口包含「本设备」(远程协助)和「账号」:未登录时显示扫码二维码,登录后显示账号(手机号打码)和「退出登录」。
|
||||||
|
扫码登录或退出登录后,主机会自动切换模式。已登录的后台服务默认隐藏窗口,用 `uuremote-server show` 或应用菜单打开;关闭窗口只是隐藏。
|
||||||
|
One window with remote assistance and the account (QR before sign-in, account + sign out after).
|
||||||
|
- **应用图标和启动项**(#13):`contrib/desktop/` 里有图标和 `.desktop` 文件,安装后应用菜单里会出现「UU 远程 Linux 主机」。
|
||||||
|
App icon and desktop launcher.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- 服务模式下扫码登录时,二维码(登录凭证)不再打印到标准输出(会进入系统日志)。/ The QR is no longer printed to a service's stdout.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.11-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
Mac 快捷键和在 Mac 上一样用。/ Mac shortcuts work as on a Mac.
|
||||||
|
|
||||||
|
### 新增 / Added
|
||||||
|
- **Mac 快捷键**:在 Mac 远控窗口菜单里**关闭「使用 macOS 快捷键」**后:
|
||||||
|
- Cmd+C / V / A / F / X / Z 是复制、粘贴、全选、查找、剪切、撤销(Cmd+Shift+Z 等组合同样可用);
|
||||||
|
- 在终端里 Cmd+C / Cmd+V 自动变成终端的复制、粘贴(Ctrl+Shift+C / V),**Ctrl+C 始终是中断**;
|
||||||
|
- 其他 Cmd 组合键和单独按 Cmd 仍然是 Super(Windows 键)。
|
||||||
|
只对 Mac 控制端生效(按连接请求里的控制端平台判断),Windows 和手机控制端不受影响。✅ 已用官方 Mac 客户端在 X11 实机验收。
|
||||||
|
保持该选项开启时行为和以前一样(客户端会把 Cmd 当成 Ctrl 发送)。
|
||||||
|
With 「使用 macOS 快捷键」 off, Cmd shortcuts behave as on a Mac (terminal-aware copy/paste), only for macOS viewers.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.12-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
托盘图标。/ Tray icon.
|
||||||
|
|
||||||
|
### 新增 / Added
|
||||||
|
- **托盘图标**:后台运行时,顶栏/任务栏会显示托盘图标,菜单里有「打开主窗口」、「允许他人远程协助」(与主窗口开关同步)和「退出」。
|
||||||
|
- GNOME 通过 AppIndicator 显示(Ubuntu 自带扩展;运行时加载 libayatana-appindicator,无需额外编译依赖);Xfce / Cinnamon / KDE 等使用系统托盘。
|
||||||
|
- 「退出」会停止 `uuremote-native.service`(不会被自动重启);之后从应用菜单「UU 远程 Linux 主机」打开,会重新启动服务并显示主窗口。
|
||||||
|
✅ Ubuntu 24.04 GNOME(X11)实机验收:图标、菜单、开关同步、退出、从应用菜单重新启动。⚠️ Xfce 等系统托盘路径尚未实机验收。
|
||||||
|
Tray icon with open / allow / quit; Quit stops the user service and the launcher restarts it. Verified on Ubuntu GNOME.
|
||||||
|
|
||||||
|
### 改进 / Improved
|
||||||
|
- `uuremote-server show`:主机没在运行时,优先启动已安装的 `uuremote-native.service`,而不是另起一个不受管理的进程。
|
||||||
|
- 修复启动时的一个时序问题:先注册窗口唤起信号再写入 PID 文件,避免过早的 `show` 意外结束主机进程。
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.13-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
Wayland(GNOME / KDE portal)修复版本。X11 行为不变。
|
||||||
|
Wayland (GNOME / KDE portal) fix release. X11 behavior is unchanged.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- **GNOME Wayland 首次连接黑屏,桌面重绘后才恢复**(#2):新建的 portal 会话会把第一帧有效画面当作锁屏残留清掉,
|
||||||
|
静止的桌面又不会产生新帧,所以画面一直是黑的。现在只有真正检测到锁屏(或 portal 被关闭)时才丢弃旧画面。
|
||||||
|
First connection on GNOME Wayland stayed black until the desktop redrew (#2): a fresh portal session discarded its
|
||||||
|
first valid frame as if it came from the lock screen.
|
||||||
|
|
||||||
|
### 其他 / Other
|
||||||
|
- CI 新增 `--features wayland,portal` 的编译和测试。/ CI now builds and tests `--features wayland,portal`.
|
||||||
|
|
||||||
|
### 已知问题 / Known issues
|
||||||
|
- Wayland(portal)下 Mac 控制端输入的下划线可能丢失(#3,正在排查,与键盘布局有关)。
|
||||||
|
- Wayland 下剪贴板只有「控制端 → Linux」方向可用;长连接约 20 分钟可能被云端释放(排查中)。
|
||||||
|
- Wayland 支持仍为实验性,需从源码以 `--features wayland,portal` 编译。
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.3-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35)。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
新增端口映射。/ Adds port mapping.
|
||||||
|
|
||||||
|
### 新增 / Added
|
||||||
|
- **端口映射**:支持官方客户端的「端口映射」工具。在控制端添加规则(本地端口 → 目标地址:端口),即可把控制端本地端口转发到
|
||||||
|
Linux 被控端能访问到的地址,例如 `127.0.0.1:22`(SSH)或局域网里的其他服务。上传、下载、多连接并发均已用官方 Mac 客户端验收。
|
||||||
|
设置 `UU_PORT_MAPPING=0` 可关闭。
|
||||||
|
**Port mapping**: the official "port mapping" tool now works — forward a local port on the viewer to any address reachable
|
||||||
|
from the Linux host (e.g. `127.0.0.1:22`). Upload, download and concurrent streams verified with the official Mac client.
|
||||||
|
`UU_PORT_MAPPING=0` disables it.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- 数据通道单条消息上限从 64 KB 提升到 256 KB(与官方被控端一致)。此前控制端发送的大于 64 KB 的消息会被拒发,导致上传卡住。
|
||||||
|
Data-channel messages up to 256 KB are now accepted (as the official host advertises); larger viewer messages were refused before.
|
||||||
|
- 画质/帧率/分辨率切换时,旧编码器的帧可能插到新编码器的关键帧之后,导致画面花屏(#5,感谢 @BLACKIELF)。
|
||||||
|
Encoder restart ordering on quality/fps/resolution changes (#5, thanks @BLACKIELF).
|
||||||
|
|
||||||
|
### 其他 / Other
|
||||||
|
- 可选的进程内编码(`--features inproc-av`,默认关闭;Release 安装包不受影响)(#6,感谢 @ferstar)。
|
||||||
|
Optional in-process encoding (`--features inproc-av`, off by default) (#6, thanks @ferstar).
|
||||||
|
|
||||||
|
### 已知问题 / Known issues
|
||||||
|
- 控制端不支持 TCP 半关闭(发送方关闭写方向后会直接关闭整条连接);SSH、HTTP 等常见协议不受影响。
|
||||||
|
The viewer does not support TCP half-close; common protocols such as SSH and HTTP are unaffected.
|
||||||
|
- Wayland 仍为实验性;portal 下「Linux → 控制端」剪贴板和约 20 分钟后的云端断开仍在排查。
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.4-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35)。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
修复长时间远控约 20 分钟后被断开的问题。/ Fixes sessions being dropped after ~20 minutes.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- **远控约 20 分钟后被云端断开**:官方客户端会定期(约每 5 分钟)通过信令刷新房间的重连凭证(`refresh_reconnect_key`),
|
||||||
|
之前没有实现,云端大约 20 分钟后就会回收房间并断开连接。现在被控端会按官方节奏刷新;凭证只保存在内存中,不写日志。
|
||||||
|
在测试虚拟机里长连接稳定运行 44 分钟以上(已越过原来的断开点),在 X11 实机上确认刷新被云端接受。
|
||||||
|
Sessions were released by the cloud after ~20 minutes because the room's reconnect key was never refreshed. The host now
|
||||||
|
refreshes it like the official clients (~every 5 minutes); the key stays in memory and is never logged.
|
||||||
|
- 旧的信令连接在重连后会被正确关闭。/ Stale signaling connections are closed after a reconnect.
|
||||||
|
|
||||||
|
### 其他 / Other
|
||||||
|
- 可选的进程内编码路径支持 XDamage:桌面静止时几乎不占 CPU 和带宽(`--features inproc-av`,默认关闭)(#7,感谢 @ferstar)。
|
||||||
|
XDamage capture on the opt-in in-process path (#7, thanks @ferstar).
|
||||||
|
- Wayland(portal)下双向剪贴板已在虚拟机中验收通过。注意:官方 Mac 客户端只在远控窗口处于前台(获得焦点)时接收被控端的剪贴板。
|
||||||
|
Two-way clipboard on the portal backend passes in the VM; the official Mac viewer only accepts the host clipboard while the
|
||||||
|
remote window has focus.
|
||||||
|
- 更多信令诊断日志(不含任何凭证)。/ More signaling diagnostics (no credentials).
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.5-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35)。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
无头服务器、虚拟桌面、uinput 输入和终端快捷键。/ Headless servers, virtual desktop, uinput input and terminal shortcuts.
|
||||||
|
|
||||||
|
### 新增 / Added
|
||||||
|
- **无头模式** `UU_DESKTOP_BACKEND=none`:没有显示器、没有图形会话也能上线,提供终端、文件传输和端口映射。
|
||||||
|
配合 `loginctl enable-linger` 可以开机自动上线。已在实机上用官方 Mac 客户端验收(端口映射传输 10 MB 校验一致)。
|
||||||
|
Headless mode: online without a display; terminal, file transfer and port mapping verified with the official Mac viewer.
|
||||||
|
- **虚拟桌面** `contrib/virtual-desktop/`:用 Xvfb 创建虚拟屏幕并启动桌面会话,没有显示器的服务器也能使用完整的远程桌面。
|
||||||
|
实机验收了画面和键鼠;默认的「用户总线」模式(把 `DISPLAY` 导入 systemd 用户会话)尚未在纯无头机器上验证。
|
||||||
|
Virtual desktop scripts (Xvfb + session). Video and input verified; the default user-bus mode is not yet verified on a
|
||||||
|
truly monitorless machine.
|
||||||
|
- **uinput 虚拟键鼠**(参考 Sunshine):Wayland/portal 下,只要当前用户能访问 `/dev/uinput`(README 里有一条 udev 规则),
|
||||||
|
就自动改用内核级虚拟键盘和鼠标,发送物理按键码,不依赖键盘布局,也不需要 portal 输入授权。应能解决 #3(下划线被吞)。
|
||||||
|
`UU_INPUT_BACKEND=auto|uinput|native`;X11 继续默认用 XTest。
|
||||||
|
uinput keyboard/mouse on Wayland (layout independent, no portal input consent); should fix #3.
|
||||||
|
- **终端快捷键**:Mac 控制端的 Cmd 和 Ctrl 到被控端都是 Ctrl。焦点在终端窗口时,Ctrl+V / Cmd+V 自动转成 Ctrl+Shift+V 粘贴;
|
||||||
|
Ctrl+C 始终是中断,复制用 Cmd+Shift+C(X11,`UU_TERMINAL_SHORTCUTS=0` 关闭)。
|
||||||
|
Terminal-aware paste: Ctrl+V/Cmd+V paste in terminal windows, Ctrl+C always interrupts.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- 没有桌面时(无头模式,或 portal 后端还没授权时)打开端口映射会被控制端立即断开:现在工具连接会收到屏幕信息和「允许」权限。
|
||||||
|
Port mapping was aborted by the viewer when no desktop was available.
|
||||||
|
- 明确设置的 `DISPLAY` 打不开时,不再悄悄改用 `:0`/`:1`(会截到真实屏幕),而是报错。
|
||||||
|
An explicit DISPLAY that cannot be opened no longer falls back to the physical screen.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.6-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/virtual-desktop/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
VAAPI 硬件编码、虚拟声卡、无人登录的虚拟桌面和 Arch/Omarchy 支持。/ VAAPI encoding, virtual audio sink, linger virtual desktop, Arch/Omarchy.
|
||||||
|
|
||||||
|
### 新增 / Added
|
||||||
|
- **VAAPI 硬件编码(Intel/AMD)**:`UU_VIDEO_ENCODER=vaapi`(`UU_VAAPI_DEVICE` 默认 `/dev/dri/renderD128`),需要显式开启,不会自动选择;
|
||||||
|
初始化失败时自动回退 CPU 编码。✅ Intel UHD 770(iHD 驱动)实机验收:用官方 Mac 控制端测试了各档画质、30/60 帧、缩放和重连,
|
||||||
|
全程硬件编码,CPU 占用比 libx264 低约 40%。⚠️ AMD 显卡、`--features inproc-av` 管线的 VAAPI 尚未实机验证。
|
||||||
|
Opt-in VAAPI with CPU fallback. Accepted on Intel UHD 770; AMD and the inproc-av path are not hardware-verified.
|
||||||
|
- **虚拟声卡(静音运行)**:开启「静音运行」时创建虚拟输出 `uu-remote-sink`,把声音转到它上面采集,本机扬声器静音、控制端照常有声;
|
||||||
|
结束后恢复原默认输出;异常退出后下次启动自动清理。有 `pactl` 时用它,没有时(默认的 Ubuntu 桌面)用 PipeWire 自带的 `pw-cli`/`wpctl`。
|
||||||
|
✅ Ubuntu 24.04(PipeWire 1.0.5)上用官方 Mac 控制端验收:开关、断开恢复、多工具连接,以及强杀后的恢复。
|
||||||
|
Virtual null sink while muted (pactl or PipeWire-native). Accepted on Ubuntu 24.04 with the official Mac viewer.
|
||||||
|
- **虚拟桌面改进**:默认使用 linger 用户总线,并导入 `DISPLAY`/`XAUTHORITY`,D-Bus 激活的程序(如 GNOME Terminal)也会出现在虚拟屏幕上;
|
||||||
|
独立的 X 认证 cookie、`Type=notify` 就绪通知、退出清理;Xvfb 被强杀后残留的显示器文件会自动清理。
|
||||||
|
✅ 虚拟机里验证了无人登录开机上线、GNOME Terminal 出现在 `:99`、键鼠;残留清理在实机验证。
|
||||||
|
⚠️ 无人登录场景下尚未用官方控制端完成验收(清单见 `contrib/virtual-desktop/ACCEPTANCE.md`)。
|
||||||
|
- **Arch Linux / Omarchy**:README 增加 pacman 依赖和 Omarchy(Hyprland)说明。✅ 虚拟机里 Hyprland 截屏、uinput 键鼠、剪贴板通过。
|
||||||
|
⚠️ 尚未在真实 Omarchy 上用官方控制端验收;相对鼠标位移的精确像素没有验证。
|
||||||
|
- **KMS 截屏(实验原型,默认不编译)**:`--features kms` + `UU_DESKTOP_BACKEND=kms`,需要 `cap_sys_admin`。
|
||||||
|
❌ 虚拟机里登录界面截图全黑,视觉验收未通过;**不支持**锁屏/登录界面,发布包里也不包含这个功能。
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- 虚拟声卡恢复时,如果原默认声卡已经不存在(比如 USB 声卡被拔掉),不再留下虚拟输出作为默认。
|
||||||
|
- 「静音运行」相关的日志会区分虚拟输出和真实声卡的静音状态。
|
||||||
|
|
||||||
|
### 未改变 / Unchanged
|
||||||
|
- 无头模式下打开远程桌面仍会显示「正在重新连接」:没找到官方的「无桌面」错误码,不挪用其他错误码。
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.7-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
修复大分辨率 CPU 编码时画面周期性变灰,以及非 NVIDIA 机器上每次建流都白跑一次 NVENC。/ Fixes grey keyframes on large CPU-encoded desktops and needless NVENC attempts on non-NVIDIA hosts.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- **大分辨率 CPU 编码画面周期性变灰**(#8,感谢 @liangjiahui01 的详细分析):libx264 的 VBV 缓冲原来最多 500 kbit,
|
||||||
|
3296×2036 这类大屏的关键帧放不下,x264 只能输出近乎纯灰的关键帧再逐步补清晰。现在 CPU 编码的缓冲改为 1 秒的码率
|
||||||
|
(500k~8000k),NVENC 和 VAAPI 不变。用 3296×2036 静态画面、高清 2M 档复现验证:原来每个关键帧都变灰,修复后不再出现。
|
||||||
|
x264 VBV buffer is now one second of bitrate; NVENC/VAAPI unchanged.
|
||||||
|
- **非 NVIDIA 机器每次建流都先试一次 NVENC**(#10,感谢 @hiclaw):`ffmpeg -encoders` 在没有 N 卡时也会列出 `h264_nvenc`,
|
||||||
|
所以以前 AMD/Intel 机器上每次连接或切换画质都会先启动一个注定失败的 NVENC 编码。现在未设置 `UU_VIDEO_ENCODER` 时,
|
||||||
|
只有检测到 NVIDIA 驱动才尝试 NVENC,否则直接用 CPU 编码;新增 `UU_VIDEO_ENCODER=nvenc` 可以强制使用。
|
||||||
|
NVENC is only tried when an NVIDIA driver is present; `UU_VIDEO_ENCODER=nvenc` forces it.
|
||||||
|
|
||||||
|
### 文档 / Docs
|
||||||
|
- README 的 Wayland 限制里写明:Hyprland 原生后端通过 shm screencopy 截屏,帧率受合成器限制(2560×1600 实测约 16~22 帧),
|
||||||
|
控制端的帧率选项不生效(#9,后续会尝试 dmabuf 零拷贝)。
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.8-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
修复连接断开或重启后鼠标/按键卡住,Hyprland 截屏改为流水线。/ Fixes stuck mouse buttons/keys after disconnects or restarts; pipelined Hyprland capture.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- **鼠标键或按键卡在按下状态,导致之后点击全部无效**:如果远程连接在按住鼠标或按键时断开,或者服务重启,按下状态可能残留,
|
||||||
|
之后的点击和输入都会出错。现在桌面连接建立、断开以及服务启动/退出时,会自动松开所有按住的键和鼠标键
|
||||||
|
(X11 松开显示器上所有按下的键;uinput 重置本服务的虚拟设备;Wayland/portal 松开本服务能发送的所有键)。
|
||||||
|
旧连接迟到的事件不会影响新连接;文件传输、终端、端口映射这些工具连接不会触发松键。
|
||||||
|
✅ X11 实机(官方 Mac 控制端,按住鼠标时关闭远控窗口后重连)和虚拟机验收通过;portal 只做了编译和单元测试。
|
||||||
|
Input is released at desktop session boundaries and at service start/stop. Verified on X11 hardware; portal unit-tested only.
|
||||||
|
注意:X11 下会同时松开本机键盘上正在按住的键。/ On X11 this also releases keys held on the local keyboard.
|
||||||
|
|
||||||
|
### 改进 / Improved
|
||||||
|
- **Hyprland 原生后端(`--features wayland`)**:截屏改为双缓冲流水线,控制端选择的帧率会同步到截屏和编码
|
||||||
|
(#9)。可选 `UU_WAYLAND_DMABUF=1` 尝试 dma-buf 映射(默认关闭,只支持线性缓冲,失败回退 shm);`UU_WAYLAND_PIPELINE=0` 关闭流水线。
|
||||||
|
⚠️ 只在虚拟机里测量过,实机帧率提升尚未验证,实际帧率仍受合成器限制。发布包(X11 版)不含此功能。
|
||||||
|
Pipelined screencopy and viewer-selected fps for the native Wayland backend (VM-measured only).
|
||||||
|
- **AMD VAAPI**:#9 的报告者在 AMD Phoenix 上实测 v0.1.7 的 `UU_VIDEO_ENCODER=vaapi` 有效,消除了高分辨率下的顿挫感。
|
||||||
|
AMD VAAPI confirmed by a user on AMD Phoenix.
|
||||||
|
- 无人登录的虚拟桌面(`contrib/virtual-desktop`)在虚拟机里用官方 Mac 控制端验收了画面、键鼠、剪贴板(虚拟机→Mac)、
|
||||||
|
静音运行的声音和会话结束后的恢复。/ Linger virtual desktop accepted in a VM with the official Mac viewer.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.9-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
+536
@@ -0,0 +1,536 @@
|
|||||||
|
//! Opt-in guest assistance. Protocol sources: docs/validation/remote-assist-evidence.md.
|
||||||
|
//! Secret values never implement Display/Debug or enter diagnostics.
|
||||||
|
use crate::auth::{Credentials, RoomData, UUAuthClient};
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
use std::io::{Read, Write};
|
||||||
|
use std::os::unix::{fs::OpenOptionsExt, io::AsRawFd};
|
||||||
|
|
||||||
|
const CREATE: &str = "/api/v1/guest/create";
|
||||||
|
const INFO: &str = "/api/v1/guest/share/info";
|
||||||
|
const CANCEL: &str = "/api/v1/guest/share/cancel";
|
||||||
|
const ROOM: &str = "/api/v1/guest/room/create";
|
||||||
|
const SIGN: &str = "/api/v1/guest/room/share/upload/sign";
|
||||||
|
const CONTROL_MODE: &str = "/api/v1/guest/room/share/upload_control_mode";
|
||||||
|
// Logged-in host: the same flow on the account identity (official Windows host NrdDevice/NrdBusinessEntry).
|
||||||
|
const ACCOUNT_INFO: &str = "/api/v1/device/share/info";
|
||||||
|
const ACCOUNT_CANCEL: &str = "/api/v1/device/share/cancel";
|
||||||
|
const ACCOUNT_SIGN: &str = "/api/v2/room/share/upload_sign";
|
||||||
|
const ACCOUNT_CONTROL_MODE: &str = "/api/v2/room/share/upload_control_mode";
|
||||||
|
const ALPHABET: &[u8] = b"ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
||||||
|
|
||||||
|
/// Guest mode uses its own device/config identity and never consumes an account token.
|
||||||
|
pub fn configure() -> Result<(), String> {
|
||||||
|
if ["UU_USER_ID", "UU_TOKEN", "UU_CSRF"].iter().any(|k| std::env::var_os(k).is_some_and(|v| !v.is_empty())) {
|
||||||
|
return Err("assist mode cannot use account credential environment variables".into());
|
||||||
|
}
|
||||||
|
if std::env::var_os("UU_CONFIG").is_none() {
|
||||||
|
if crate::config::read_fresh()?.as_ref().is_some_and(|v| ["token", "user_id"].iter()
|
||||||
|
.any(|k| v.get(k).and_then(Value::as_str).is_some_and(|s| !s.is_empty()))) {
|
||||||
|
return Err("account identity already configured; guest assist needs an explicit separate UU_CONFIG".into());
|
||||||
|
}
|
||||||
|
let path = crate::config::path().with_file_name("assist").join("config.json");
|
||||||
|
std::env::set_var("UU_CONFIG", path);
|
||||||
|
}
|
||||||
|
if crate::config::read_fresh()?.as_ref().is_some_and(|v| ["token", "user_id", "csrf"].iter()
|
||||||
|
.any(|k| v.get(k).and_then(Value::as_str).is_some_and(|s| !s.is_empty()))) {
|
||||||
|
return Err("assist requires a separate config without account credentials".into());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Official RecipientConfigModel.verificationType: temporary only, custom only, or both.
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||||
|
pub enum Verify { Temporary = 0, Custom = 1, Both = 2 }
|
||||||
|
impl Verify {
|
||||||
|
fn parse(v: &str) -> Option<Self> { match v { "temporary" => Some(Self::Temporary), "custom" => Some(Self::Custom), "both" => Some(Self::Both), _ => None } }
|
||||||
|
fn name(self) -> &'static str { match self { Self::Temporary => "temporary", Self::Custom => "custom", Self::Both => "both" } }
|
||||||
|
fn from_index(i: i64) -> Option<Self> { match i { 0 => Some(Self::Temporary), 1 => Some(Self::Custom), 2 => Some(Self::Both), _ => None } }
|
||||||
|
}
|
||||||
|
/// Official RecipientCodeRefreshType: after each session (closeConnect), hourly, 12 h, 24 h, never.
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||||
|
pub enum Refresh { Session = 0, Hour = 1, Hour12 = 2, Hour24 = 3, Never = 4 }
|
||||||
|
impl Refresh {
|
||||||
|
fn parse(v: &str) -> Option<Self> { match v { "session" => Some(Self::Session), "1h" => Some(Self::Hour), "12h" => Some(Self::Hour12), "24h" => Some(Self::Hour24), "never" => Some(Self::Never), _ => None } }
|
||||||
|
fn name(self) -> &'static str { match self { Self::Session => "session", Self::Hour => "1h", Self::Hour12 => "12h", Self::Hour24 => "24h", Self::Never => "never" } }
|
||||||
|
fn from_index(i: i64) -> Option<Self> { [Self::Session, Self::Hour, Self::Hour12, Self::Hour24, Self::Never].get(usize::try_from(i).ok()?).copied() }
|
||||||
|
fn period(self) -> Option<std::time::Duration> {
|
||||||
|
match self { Self::Hour => Some(3600), Self::Hour12 => Some(12 * 3600), Self::Hour24 => Some(24 * 3600), _ => None }.map(std::time::Duration::from_secs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Mainland mobile numbers are shown like the official client: 186******08.
|
||||||
|
pub fn display_account(name: &str) -> String {
|
||||||
|
if name.len() == 11 && name.bytes().all(|b| b.is_ascii_digit()) { format!("{}******{}", &name[..3], &name[9..]) } else { name.to_owned() }
|
||||||
|
}
|
||||||
|
/// 8-16 ASCII letters/digits including both (official RecipientCustomCodeView rules).
|
||||||
|
fn valid_custom(code: &str) -> bool {
|
||||||
|
(8..=16).contains(&code.len()) && code.bytes().all(|b| b.is_ascii_alphanumeric())
|
||||||
|
&& code.bytes().any(|b| b.is_ascii_alphabetic()) && code.bytes().any(|b| b.is_ascii_digit())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Command { Allow, Deny, Reset, Stop, Verify(Verify), Refresh(Refresh), Custom(String), Logout, Quit }
|
||||||
|
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
extern "C" {
|
||||||
|
fn uu_assist_ui_start(hidden: libc::c_int, hide_on_close: libc::c_int) -> libc::c_int;
|
||||||
|
fn uu_assist_ui_present();
|
||||||
|
fn uu_ui_set_user(name: *const libc::c_char);
|
||||||
|
fn uu_assist_ui_set(id: *const libc::c_char, code: *const libc::c_char, allowed: libc::c_int, verify: libc::c_int, refresh: libc::c_int, has_custom: libc::c_int);
|
||||||
|
fn uu_assist_ui_take_command(arg: *mut libc::c_char, len: libc::size_t) -> libc::c_int;
|
||||||
|
fn uu_assist_ui_close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// "243829726" -> "243 829 726", as the official client shows device IDs.
|
||||||
|
fn display_id(id: &str) -> String {
|
||||||
|
if id.is_empty() || !id.bytes().all(|b| b.is_ascii_digit()) { return id.to_owned(); }
|
||||||
|
id.as_bytes().chunks(3).map(|c| String::from_utf8_lossy(c).into_owned()).collect::<Vec<_>>().join(" ")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Where the ID/code are shown and commands come from: a local GTK window (c_engine/assist_window.c)
|
||||||
|
/// when a display is available, otherwise /dev/tty, which bypasses stdout/stderr, journals,
|
||||||
|
/// redirected output and the logger. Without either, refuse startup rather than disclose the code.
|
||||||
|
pub enum LocalUi { Tty(tokio::io::unix::AsyncFd<std::fs::File>), #[cfg(uu_gtk)] Window }
|
||||||
|
impl LocalUi {
|
||||||
|
/// Standalone `assist`: visible window (closing it stops assistance), else /dev/tty.
|
||||||
|
pub fn open() -> Result<Self, String> { Self::open_visible(false) }
|
||||||
|
/// `hide_on_close`: the not-logged-in host service (window stays reachable with `uuremote-server show`).
|
||||||
|
pub fn open_visible(hide_on_close: bool) -> Result<Self, String> {
|
||||||
|
#[cfg(not(uu_gtk))] let _ = hide_on_close;
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
if std::env::var("UU_ASSIST_UI").ok().as_deref() != Some("tty") && Self::display() && unsafe { uu_assist_ui_start(0, i32::from(hide_on_close)) } == 0 {
|
||||||
|
return Ok(Self::Window);
|
||||||
|
}
|
||||||
|
let tty = std::fs::OpenOptions::new().read(true).write(true)
|
||||||
|
.custom_flags(libc::O_NONBLOCK | libc::O_NOCTTY).open("/dev/tty")
|
||||||
|
.map_err(|_| "assist requires a desktop window or a local controlling terminal; no code was printed")?;
|
||||||
|
if unsafe { libc::isatty(tty.as_raw_fd()) } != 1 { return Err("assist output is not a terminal".into()); }
|
||||||
|
Ok(Self::Tty(tokio::io::unix::AsyncFd::new(tty).map_err(|_| "assist terminal registration failed")?))
|
||||||
|
}
|
||||||
|
fn display() -> bool { std::env::var_os("DISPLAY").is_some() || std::env::var_os("WAYLAND_DISPLAY").is_some() }
|
||||||
|
/// Host service: a hidden window opened on demand (`uuremote-server show`); closing only hides it.
|
||||||
|
/// No terminal fallback: a background service has nowhere safe to print the code.
|
||||||
|
pub fn open_service() -> Result<Self, String> {
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
if Self::display() && unsafe { uu_assist_ui_start(1, 1) } == 0 { return Ok(Self::Window); }
|
||||||
|
Err("remote assistance needs a desktop window for the host service".into())
|
||||||
|
}
|
||||||
|
/// Account card: signed-in user (name already masked for display).
|
||||||
|
pub fn set_user(&self, name: &str) {
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
if matches!(self, Self::Window) { let name = std::ffi::CString::new(name).unwrap_or_default(); unsafe { uu_ui_set_user(name.as_ptr()) } }
|
||||||
|
#[cfg(not(uu_gtk))] let _ = name;
|
||||||
|
}
|
||||||
|
pub fn present(&self) {
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
if matches!(self, Self::Window) { unsafe { uu_assist_ui_present() } }
|
||||||
|
}
|
||||||
|
fn show(&self, id: &str, code: &str, allowed: bool, verify: Verify, refresh: Refresh, has_custom: bool) -> Result<(), String> {
|
||||||
|
#[cfg(not(uu_gtk))] let _ = has_custom;
|
||||||
|
match self {
|
||||||
|
Self::Tty(tty) => {
|
||||||
|
// Bound/control-character-validated server identifier; no terminal escape injection.
|
||||||
|
let mut tty = tty.get_ref();
|
||||||
|
write!(tty, "\n远程协助 / Remote assistance\n设备 ID: {}\n临时验证码: {code}\n允许他人远程协助: {allowed}\n验证方式: {} 自动刷新: {}\n仅向可信的人提供验证码。allow=允许,deny=不允许,reset=换码,mode temporary|custom|both,refresh session|1h|12h|24h|never,off=关闭并撤销\n", display_id(id), verify.name(), refresh.name())
|
||||||
|
.map_err(|_| "assist terminal display failed".into())
|
||||||
|
}
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
Self::Window => {
|
||||||
|
let (id, code) = (std::ffi::CString::new(display_id(id)).unwrap_or_default(), std::ffi::CString::new(code).unwrap_or_default());
|
||||||
|
unsafe { uu_assist_ui_set(id.as_ptr(), code.as_ptr(), i32::from(allowed), verify as i32, refresh as i32, i32::from(has_custom)) };
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub async fn command(&self) -> Command {
|
||||||
|
match self {
|
||||||
|
Self::Tty(tty) => Self::tty_command(tty).await,
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
Self::Window => loop {
|
||||||
|
let mut arg = [0 as libc::c_char; 40];
|
||||||
|
let cmd = unsafe { uu_assist_ui_take_command(arg.as_mut_ptr(), arg.len()) };
|
||||||
|
let text = unsafe { std::ffi::CStr::from_ptr(arg.as_ptr()) }.to_string_lossy().into_owned();
|
||||||
|
let index = text.parse::<i64>().unwrap_or(-1);
|
||||||
|
match cmd {
|
||||||
|
1 => return Command::Allow, 2 => return Command::Deny, 3 => return Command::Reset, 4 => return Command::Stop,
|
||||||
|
5 => if let Some(v) = Verify::from_index(index) { return Command::Verify(v) },
|
||||||
|
6 => if let Some(r) = Refresh::from_index(index) { return Command::Refresh(r) },
|
||||||
|
7 => return Command::Custom(text),
|
||||||
|
8 => return Command::Logout,
|
||||||
|
9 => return Command::Quit,
|
||||||
|
_ => tokio::time::sleep(std::time::Duration::from_millis(150)).await,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async fn tty_command(tty: &tokio::io::unix::AsyncFd<std::fs::File>) -> Command {
|
||||||
|
let mut line = Vec::new();
|
||||||
|
loop {
|
||||||
|
let mut ready = match tty.readable().await { Ok(v) => v, Err(_) => return Command::Stop };
|
||||||
|
let result = ready.try_io(|fd| { let mut byte = [0u8]; let mut tty = fd.get_ref(); tty.read(&mut byte).map(|n| (n, byte[0])) });
|
||||||
|
match result {
|
||||||
|
Ok(Ok((0, _))) | Ok(Err(_)) => return Command::Stop,
|
||||||
|
Ok(Ok((_, byte))) if byte == b'\n' => {
|
||||||
|
let text = String::from_utf8_lossy(&line).trim().to_owned();
|
||||||
|
let cmd = match text.split_once(' ') {
|
||||||
|
Some(("mode", v)) => Verify::parse(v).map(Command::Verify),
|
||||||
|
Some(("refresh", v)) => Refresh::parse(v).map(Command::Refresh),
|
||||||
|
_ => match text.as_str() { "allow" => Some(Command::Allow), "deny" => Some(Command::Deny), "reset" => Some(Command::Reset), "off" | "q" => Some(Command::Stop), _ => None },
|
||||||
|
};
|
||||||
|
line.clear(); if let Some(cmd) = cmd { return cmd; }
|
||||||
|
}
|
||||||
|
Ok(Ok((_, byte))) => { if line.len() < 64 { line.push(byte); } },
|
||||||
|
Err(_) => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Drop for LocalUi {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
if matches!(self, Self::Window) { unsafe { uu_assist_ui_close() } }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn response(status: u16, value: &Value) -> Result<&Value, String> {
|
||||||
|
let code = value.get("code").and_then(Value::as_i64).unwrap_or(-1);
|
||||||
|
if status == 200 && code == 0 { return Ok(value.get("data").unwrap_or(&Value::Null)); }
|
||||||
|
// No server message, validation payload, headers, URLs, token or code in errors. UU_ASSIST_DEBUG=1 adds
|
||||||
|
// the server's short human-readable message (bounded) for diagnosing rejected guest calls.
|
||||||
|
let message = (std::env::var("UU_ASSIST_DEBUG").ok().as_deref() == Some("1"))
|
||||||
|
.then(|| value.get("message").or_else(|| value.get("msg")).and_then(Value::as_str)
|
||||||
|
.map(|m| m.chars().filter(|c| !c.is_control()).take(120).collect::<String>()))
|
||||||
|
.flatten().map(|m| format!(" message={m:?}")).unwrap_or_default();
|
||||||
|
Err(format!("assist API rejected request: http={status} code={code}{message}"))
|
||||||
|
}
|
||||||
|
fn text(value: &Value, key: &str) -> Result<String, String> {
|
||||||
|
value.get(key).and_then(Value::as_str).filter(|s| !s.is_empty() && s.len() <= 16384)
|
||||||
|
.map(str::to_owned).ok_or_else(|| "assist response missing required string".into())
|
||||||
|
}
|
||||||
|
fn share_id(value: &Value) -> Result<String, String> {
|
||||||
|
let id = text(value, "connect_id")?;
|
||||||
|
if id.len() > 128 || !id.bytes().all(|c| c.is_ascii_alphanumeric() || c == b'-') {
|
||||||
|
return Err("assist response invalid device identifier".into());
|
||||||
|
}
|
||||||
|
Ok(id)
|
||||||
|
}
|
||||||
|
fn generate_code() -> Result<String, String> {
|
||||||
|
let mut random = std::fs::File::open("/dev/urandom").map_err(|_| "assist random source unavailable")?;
|
||||||
|
let mut code = String::new();
|
||||||
|
// Rejection sampling avoids modulo bias (alphabet has 32 entries).
|
||||||
|
let limit = 256 / ALPHABET.len() * ALPHABET.len();
|
||||||
|
while code.len() < 8 {
|
||||||
|
let mut byte = [0u8]; random.read_exact(&mut byte).map_err(|_| "assist random source failed")?;
|
||||||
|
if (byte[0] as usize) < limit { code.push(ALPHABET[byte[0] as usize % ALPHABET.len()] as char); }
|
||||||
|
}
|
||||||
|
Ok(code)
|
||||||
|
}
|
||||||
|
fn digest(salt: &str, code: &str) -> String {
|
||||||
|
let mut hash = Sha256::new(); hash.update(salt.as_bytes()); hash.update(code.as_bytes()); hex::encode(hash.finalize())
|
||||||
|
}
|
||||||
|
/// Official: temporary-only signs the temporary code with an empty backup; both adds the custom code
|
||||||
|
/// as `backup_sign`; custom-only puts the custom code in the primary `sign`.
|
||||||
|
fn sign_body(control_id: &str, salt: &str, temporary: &str, custom: Option<&str>, verify: Verify, allow: bool) -> Value {
|
||||||
|
let (primary, backup) = match (verify, custom) {
|
||||||
|
(Verify::Custom, Some(c)) => (c, None),
|
||||||
|
(Verify::Both, Some(c)) => (temporary, Some(c)),
|
||||||
|
_ => (temporary, None),
|
||||||
|
};
|
||||||
|
json!({"can_remote_control": allow, "control_id": control_id,
|
||||||
|
"sign": digest(salt, primary), "backup_sign": backup.map(|c| digest(salt, c)).unwrap_or_default()})
|
||||||
|
}
|
||||||
|
fn challenge(value: &Value) -> Option<(&str, &str)> {
|
||||||
|
let id = value.get("control_id")?.as_str()?; let salt = value.get("salt")?.as_str()?;
|
||||||
|
(!id.is_empty() && id.len() <= 256 && !salt.is_empty() && salt.len() <= 4096).then_some((id, salt))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct RetryBackoff(u64);
|
||||||
|
impl RetryBackoff {
|
||||||
|
fn next(&mut self) -> std::time::Duration { self.0 = (self.0 + 15).min(99); std::time::Duration::from_secs(self.0) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Assist {
|
||||||
|
_lock: std::fs::File,
|
||||||
|
pub renewed: bool,
|
||||||
|
device: Credentials,
|
||||||
|
pub client: std::sync::Arc<UUAuthClient>,
|
||||||
|
ui: std::sync::Arc<LocalUi>,
|
||||||
|
code: String,
|
||||||
|
custom: Option<String>,
|
||||||
|
id: String,
|
||||||
|
pub allowed: bool,
|
||||||
|
verify: Verify,
|
||||||
|
refresh: Refresh,
|
||||||
|
code_at: std::time::Instant,
|
||||||
|
/// Logged-in host: account endpoints and the host's own room (no guest identity).
|
||||||
|
account: bool,
|
||||||
|
}
|
||||||
|
impl Assist {
|
||||||
|
pub async fn start(shutdown: &mut tokio::sync::watch::Receiver<bool>, service: bool) -> Result<Self, String> {
|
||||||
|
let ui = std::sync::Arc::new(LocalUi::open_visible(service)?);
|
||||||
|
let lock = Self::lock()?;
|
||||||
|
// guest/create rejects an unregistered device (HTTP 422 code 1001, verified live): register the
|
||||||
|
// Windows device identity first, exactly as account login does (no account involved).
|
||||||
|
let (config, device) = crate::login::prepare_device().await.map_err(|_| "assist device registration failed")?;
|
||||||
|
let (custom, verify, refresh, allowed) = Self::settings(&config)?;
|
||||||
|
let mut retry = RetryBackoff::default();
|
||||||
|
let client = loop {
|
||||||
|
if *shutdown.borrow() { return Err("assist stopped".into()); }
|
||||||
|
let result = tokio::select! {
|
||||||
|
result = Self::authenticate(&device) => result,
|
||||||
|
_ = shutdown.changed() => return Err("assist stopped".into()),
|
||||||
|
_ = ui.command() => return Err("assist stopped before guest identity was available".into()),
|
||||||
|
};
|
||||||
|
match result { Ok(client) => break client, Err(error) => {
|
||||||
|
let delay = retry.next();
|
||||||
|
// `error` carries only HTTP status / API code numbers or a fixed transport label.
|
||||||
|
log::warn!("Guest identity request failed: {error}; retry_s={}", delay.as_secs());
|
||||||
|
tokio::select! {
|
||||||
|
_ = tokio::time::sleep(delay) => {},
|
||||||
|
_ = shutdown.changed() => return Err("assist stopped".into()),
|
||||||
|
_ = ui.command() => return Err("assist stopped before guest identity was available".into()),
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
};
|
||||||
|
let this = Self { _lock: lock, renewed: false, device, client: std::sync::Arc::new(client), ui, code: generate_code()?, custom, id: String::new(),
|
||||||
|
allowed, verify, refresh, code_at: std::time::Instant::now(), account: false };
|
||||||
|
// guest/share/info answers 400/2004 until a guest room exists (verified live), so the ID/code
|
||||||
|
// are published right after the first room(), which serve() requests before signaling.
|
||||||
|
Ok(this)
|
||||||
|
}
|
||||||
|
fn settings(config: &Value) -> Result<(Option<String>, Verify, Refresh, bool), String> {
|
||||||
|
let custom = config.get("assist_custom_code").and_then(Value::as_str).map(str::to_owned);
|
||||||
|
if custom.as_deref().is_some_and(|c| !valid_custom(c)) {
|
||||||
|
return Err("assist_custom_code requires 8-16 ASCII letters/digits, including both".into());
|
||||||
|
}
|
||||||
|
let setting = |key: &str| config.get(key).and_then(Value::as_str).map(str::to_owned);
|
||||||
|
let verify = setting("assist_verify").as_deref().and_then(Verify::parse).filter(|v| *v == Verify::Temporary || custom.is_some()).unwrap_or(Verify::Temporary);
|
||||||
|
let refresh = setting("assist_refresh").as_deref().and_then(Refresh::parse).unwrap_or(Refresh::Session);
|
||||||
|
// Official default is "not allowed"; the switch state persists like isAllowRecipient.
|
||||||
|
let allowed = std::env::var("UU_ASSIST_ALLOW").ok().as_deref() == Some("1")
|
||||||
|
|| config.get("assist_allow").and_then(Value::as_bool) == Some(true);
|
||||||
|
Ok((custom, verify, refresh, allowed))
|
||||||
|
}
|
||||||
|
fn lock() -> Result<std::fs::File, String> {
|
||||||
|
let lock_path = crate::config::path().with_file_name("assist.lock");
|
||||||
|
if let Some(dir) = lock_path.parent() { std::fs::create_dir_all(dir).map_err(|_| "assist config directory unavailable")?; }
|
||||||
|
let lock = std::fs::OpenOptions::new().create(true).write(true).mode(0o600).custom_flags(libc::O_NOFOLLOW).open(lock_path)
|
||||||
|
.map_err(|_| "assist lock unavailable")?;
|
||||||
|
if unsafe { libc::flock(lock.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } != 0 { return Err("another assist host uses this config".into()); }
|
||||||
|
Ok(lock)
|
||||||
|
}
|
||||||
|
/// Remote assistance on a logged-in host, shown in the service's hidden window.
|
||||||
|
pub fn start_account(client: std::sync::Arc<UUAuthClient>, device: Credentials) -> Result<Self, String> {
|
||||||
|
let lock = Self::lock()?;
|
||||||
|
let ui = std::sync::Arc::new(LocalUi::open_service()?);
|
||||||
|
let config = crate::config::read_fresh()?.unwrap_or_else(|| json!({}));
|
||||||
|
let (custom, verify, refresh, allowed) = Self::settings(&config)?;
|
||||||
|
let this = Self { _lock: lock, renewed: false, device, client, ui, code: generate_code()?, custom, id: String::new(),
|
||||||
|
allowed, verify, refresh, code_at: std::time::Instant::now(), account: true };
|
||||||
|
this.show()?;
|
||||||
|
Ok(this)
|
||||||
|
}
|
||||||
|
pub fn is_account(&self) -> bool { self.account }
|
||||||
|
/// Logged-in host: account name for the account card (saved at login, else /api/v1/user/info).
|
||||||
|
pub async fn load_account_name(&self) {
|
||||||
|
let saved = crate::config::read_fresh().ok().flatten()
|
||||||
|
.and_then(|c| c.get("account_name").and_then(Value::as_str).map(str::to_owned)).filter(|n| !n.is_empty());
|
||||||
|
let name = match saved {
|
||||||
|
Some(name) => name,
|
||||||
|
None => match self.client.assist_get("/api/v1/user/info").await {
|
||||||
|
Ok((200, v)) => v.get("data").and_then(|d| ["nickname", "mobile", "phone"].iter()
|
||||||
|
.find_map(|k| d.get(*k).and_then(Value::as_str).filter(|s| !s.is_empty()).map(str::to_owned)))
|
||||||
|
.unwrap_or_else(|| self.device.user_id.clone()),
|
||||||
|
_ => self.device.user_id.clone(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
self.ui.set_user(&display_account(&name));
|
||||||
|
}
|
||||||
|
/// Sign out: official /api/v1/user/logout (best effort), withdraw the share, drop local credentials.
|
||||||
|
pub async fn logout(&mut self) {
|
||||||
|
let _ = self.client.assist_post("/api/v1/user/logout", None).await;
|
||||||
|
let _ = self.cancel().await;
|
||||||
|
self.save_many(&[("token", Value::Null), ("csrf", Value::Null), ("user_id", Value::Null), ("account_name", Value::Null)]);
|
||||||
|
log::info!("Signed out; restarting without an account");
|
||||||
|
}
|
||||||
|
pub fn ui(&self) -> std::sync::Arc<LocalUi> { self.ui.clone() }
|
||||||
|
fn path(&self, guest: &'static str) -> &'static str {
|
||||||
|
if !self.account { return guest; }
|
||||||
|
match guest { INFO => ACCOUNT_INFO, CANCEL => ACCOUNT_CANCEL, SIGN => ACCOUNT_SIGN, CONTROL_MODE => ACCOUNT_CONTROL_MODE, other => other }
|
||||||
|
}
|
||||||
|
/// Logged-in host: publish the share ID/code once its own room exists (same ordering as guests).
|
||||||
|
pub async fn ensure_published(&mut self) -> Result<(), String> {
|
||||||
|
if self.id.is_empty() { self.publish().await } else { Ok(()) }
|
||||||
|
}
|
||||||
|
async fn authenticate(device: &Credentials) -> Result<UUAuthClient, String> {
|
||||||
|
let client = UUAuthClient::new(device.clone());
|
||||||
|
let (status, value) = client.assist_post(CREATE, None).await?;
|
||||||
|
let data = response(status, &value)?;
|
||||||
|
Ok(UUAuthClient::new_guest(device.clone(), text(data, "guest_id")?, text(data, "token")?))
|
||||||
|
}
|
||||||
|
async fn post(&mut self, path: &'static str, body: Option<&Value>) -> Result<Value, String> {
|
||||||
|
let path = self.path(path);
|
||||||
|
let (mut status, mut value) = if path == ACCOUNT_INFO { self.client.assist_get(path).await? } else { self.client.assist_post(path, body).await? };
|
||||||
|
if self.account { return response(status, &value).map(Clone::clone); }
|
||||||
|
// Official wrapper refreshes on 1120 and retries once, rather than on a guessed TTL.
|
||||||
|
if value.get("code").and_then(Value::as_i64) == Some(crate::auth::TOKEN_INVALID) {
|
||||||
|
self.client = std::sync::Arc::new(Self::authenticate(&self.device).await?);
|
||||||
|
self.renewed = true;
|
||||||
|
(status, value) = self.client.assist_post(path, body).await?;
|
||||||
|
}
|
||||||
|
response(status, &value).map(Clone::clone)
|
||||||
|
}
|
||||||
|
async fn publish(&mut self) -> Result<(), String> {
|
||||||
|
self.id = share_id(&self.post(INFO, None).await?)?;
|
||||||
|
self.show()
|
||||||
|
}
|
||||||
|
pub async fn room(&mut self) -> Result<RoomData, String> {
|
||||||
|
let value = self.post(ROOM, None).await?;
|
||||||
|
if self.renewed || self.id.is_empty() {
|
||||||
|
if let Err(error) = self.publish().await { let _ = self.cancel().await; return Err(error); }
|
||||||
|
self.renewed = false;
|
||||||
|
}
|
||||||
|
serde_json::from_value(value).map_err(|_| "assist room response malformed".into())
|
||||||
|
}
|
||||||
|
pub async fn challenge(&mut self, value: &Value) -> Result<(), String> {
|
||||||
|
let (control, salt) = challenge(value).ok_or("assist challenge malformed")?;
|
||||||
|
self.post(SIGN, Some(&sign_body(control, salt, &self.code, self.custom.as_deref(), self.verify, self.allowed))).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
/// Answer the cloud's `get_control_mode` push (data {control_id}). Official Windows host
|
||||||
|
/// (GameViewerServer RemoteAssistCode::respRemoteGetControlMode -> NrdBusinessEntry::uploadControlMode)
|
||||||
|
/// posts {control_id, allow_control, control_mode: by_password|by_confirmation|password_confirmation}.
|
||||||
|
/// Only the temporary/custom code mode is implemented, so this host reports `by_password`.
|
||||||
|
pub async fn control_mode(&mut self, value: &Value) -> Result<(), String> {
|
||||||
|
let control = value.get("control_id").and_then(Value::as_str).filter(|s| !s.is_empty() && s.len() <= 256)
|
||||||
|
.ok_or("assist control mode request malformed")?;
|
||||||
|
self.post(CONTROL_MODE, Some(&json!({"control_id": control, "allow_control": self.allowed, "control_mode": "by_password"}))).await.map(|_| ())
|
||||||
|
}
|
||||||
|
/// The code the controller types: the custom code in custom-only mode, else the temporary one.
|
||||||
|
fn show(&self) -> Result<(), String> {
|
||||||
|
let code = match (self.verify, &self.custom) { (Verify::Custom, Some(c)) => c.as_str(), _ => self.code.as_str() };
|
||||||
|
self.ui.show(&self.id, code, self.allowed, self.verify, self.refresh, self.custom.is_some())
|
||||||
|
}
|
||||||
|
/// Saves one assist setting into the owner's 0600 config (custom code included, never logged).
|
||||||
|
fn save_many(&self, entries: &[(&str, Value)]) {
|
||||||
|
let mut cfg = std::fs::read(crate::config::path()).ok().and_then(|b| serde_json::from_slice::<Value>(&b).ok())
|
||||||
|
.filter(Value::is_object).unwrap_or_else(|| json!({}));
|
||||||
|
if let Some(map) = cfg.as_object_mut() { for (k, v) in entries { if v.is_null() { map.remove(*k); } else { map.insert((*k).to_owned(), v.clone()); } } }
|
||||||
|
if crate::login::write_config(&cfg).is_err() { log::warn!("Account settings could not be saved"); }
|
||||||
|
}
|
||||||
|
fn save(&self, key: &str, value: Value) {
|
||||||
|
// Re-read the file: config::file() is a startup snapshot that predates device registration,
|
||||||
|
// so writing it back would drop device_id (and earlier saves) and change the device ID.
|
||||||
|
let mut cfg = std::fs::read(crate::config::path()).ok().and_then(|b| serde_json::from_slice::<Value>(&b).ok())
|
||||||
|
.filter(Value::is_object).unwrap_or_else(|| json!({}));
|
||||||
|
cfg[key] = value;
|
||||||
|
if crate::login::write_config(&cfg).is_err() { log::warn!("Assist setting could not be saved: key={key}"); }
|
||||||
|
}
|
||||||
|
pub fn reset(&mut self) -> Result<(), String> { self.code = generate_code()?; self.code_at = std::time::Instant::now(); self.show() }
|
||||||
|
pub fn allow(&mut self) -> Result<(), String> { self.allowed = true; self.save("assist_allow", json!(true)); self.show() }
|
||||||
|
pub fn deny(&mut self) -> Result<(), String> { self.allowed = false; self.save("assist_allow", json!(false)); self.show() }
|
||||||
|
pub fn set_verify(&mut self, verify: Verify) -> Result<(), String> {
|
||||||
|
if verify != Verify::Temporary && self.custom.is_none() { return self.show(); } // UI asks for a code first
|
||||||
|
self.verify = verify; self.save("assist_verify", json!(verify.name())); self.show()
|
||||||
|
}
|
||||||
|
pub fn set_refresh(&mut self, refresh: Refresh) -> Result<(), String> {
|
||||||
|
self.refresh = refresh; self.code_at = std::time::Instant::now(); self.save("assist_refresh", json!(refresh.name())); self.show()
|
||||||
|
}
|
||||||
|
pub fn set_custom(&mut self, code: String) -> Result<(), String> {
|
||||||
|
if !valid_custom(&code) { return self.show(); }
|
||||||
|
self.custom = Some(code.clone()); self.save("assist_custom_code", json!(code));
|
||||||
|
if self.verify == Verify::Temporary { self.verify = Verify::Custom; self.save("assist_verify", json!(self.verify.name())); }
|
||||||
|
self.show()
|
||||||
|
}
|
||||||
|
/// After the last desktop session: rotate only in "每次远控结束" mode.
|
||||||
|
pub fn session_ended(&mut self) -> Result<(), String> { if self.refresh == Refresh::Session { self.reset() } else { Ok(()) } }
|
||||||
|
/// Periodic rotation for the hourly/12 h/24 h settings.
|
||||||
|
pub fn tick(&mut self) -> Result<(), String> {
|
||||||
|
match self.refresh.period() { Some(p) if self.code_at.elapsed() >= p => self.reset(), _ => Ok(()) }
|
||||||
|
}
|
||||||
|
pub fn commands(&self, shutdown: tokio::sync::watch::Sender<bool>) -> (tokio::sync::mpsc::Receiver<Command>, tokio::task::JoinHandle<()>) {
|
||||||
|
let (tx, rx) = tokio::sync::mpsc::channel(4);
|
||||||
|
let ui = self.ui.clone();
|
||||||
|
let task = tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
let command = tokio::select! { command = ui.command() => command, _ = tx.closed() => return };
|
||||||
|
if matches!(command, Command::Stop) { let _ = shutdown.send(true); return; }
|
||||||
|
match tx.try_send(command) {
|
||||||
|
Ok(()) | Err(tokio::sync::mpsc::error::TrySendError::Full(_)) => {},
|
||||||
|
Err(tokio::sync::mpsc::error::TrySendError::Closed(_)) => return,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
(rx, task)
|
||||||
|
}
|
||||||
|
pub async fn cancel(&mut self) -> Result<(), String> { self.post(CANCEL, None).await.map(|_| ()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[tokio::test]
|
||||||
|
async fn local_terminal_display_and_explicit_commands() {
|
||||||
|
// Private PTY pair only, never the user's actual terminal; all values synthetic.
|
||||||
|
let pair = nix::pty::openpty(None, None).unwrap();
|
||||||
|
let slave = std::fs::File::from(pair.slave);
|
||||||
|
unsafe { libc::fcntl(slave.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK); }
|
||||||
|
let ui = LocalUi::Tty(tokio::io::unix::AsyncFd::new(slave).unwrap());
|
||||||
|
let mut master = std::fs::File::from(pair.master);
|
||||||
|
ui.show("fixture-123", "FIXTURE8", false, Verify::Temporary, Refresh::Session, false).unwrap();
|
||||||
|
// A PTY may deliver the banner in several chunks: read until complete (bounded).
|
||||||
|
let mut text = String::new();
|
||||||
|
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(3);
|
||||||
|
let complete = |t: &str| t.contains("fixture-123") && t.contains("FIXTURE8") && t.contains("false") && t.contains("off=");
|
||||||
|
while !complete(&text) && std::time::Instant::now() < deadline {
|
||||||
|
let mut bytes = [0u8; 1024];
|
||||||
|
match master.read(&mut bytes) { Ok(n) if n > 0 => text.push_str(&String::from_utf8_lossy(&bytes[..n])), _ => break }
|
||||||
|
}
|
||||||
|
assert!(complete(&text));
|
||||||
|
for (input, expected) in [(b"unknown\nallow\n".as_slice(), 0), (b"reset\n".as_slice(), 1), (b"off\n".as_slice(), 2)] {
|
||||||
|
master.write_all(input).unwrap();
|
||||||
|
let command = tokio::time::timeout(std::time::Duration::from_secs(2), ui.command()).await.unwrap();
|
||||||
|
assert!(matches!((command, expected), (Command::Allow, 0) | (Command::Reset, 1) | (Command::Stop, 2)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn official_challenge_sign_and_shape() {
|
||||||
|
// Synthetic fixture, independently calculated SHA256("saltABC234XY").
|
||||||
|
let body = sign_body("fixture-control", "salt", "ABC234XY", None, Verify::Temporary, false);
|
||||||
|
assert_eq!(body.as_object().unwrap().len(), 4);
|
||||||
|
assert_eq!(body["can_remote_control"], false);
|
||||||
|
assert_eq!(body["control_id"], "fixture-control");
|
||||||
|
assert_eq!(body["sign"], "dc747ee8adb0e591f5ff4ab6cabcaf3a0ec312851580f225d4ca140766229c94");
|
||||||
|
assert_eq!(body["backup_sign"], "");
|
||||||
|
let both = sign_body("fixture", "salt", "ABC234XY", Some("test1234"), Verify::Both, true);
|
||||||
|
assert_ne!(both["sign"], both["backup_sign"]);
|
||||||
|
assert_eq!(both["can_remote_control"], true);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn rejects_missing_or_unbounded_challenges_and_terminal_injection() {
|
||||||
|
for value in [json!({}), json!({"control_id":"x", "salt":""}), json!({"control_id":12,"salt":"s"}), json!({"control_id":"x","salt":"s".repeat(4097)})] {
|
||||||
|
assert!(challenge(&value).is_none());
|
||||||
|
}
|
||||||
|
assert!(challenge(&json!({"control_id":"fixture", "salt":"fixture"})).is_some());
|
||||||
|
assert!(share_id(&json!({"connect_id":"fixture-123"})).is_ok());
|
||||||
|
for id in ["", "\u{1b}[2J", "a\nb", "x\u{7}"] { assert!(share_id(&json!({"connect_id":id})).is_err()); }
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn responses_do_not_disclose_untrusted_server_errors() {
|
||||||
|
let error = response(422, &json!({"code":1120,"msg":"synthetic-secret","data":{"token":"synthetic-secret"}})).unwrap_err();
|
||||||
|
assert!(!error.contains("synthetic-secret"));
|
||||||
|
assert!(response(200, &json!({"code":0})).unwrap().is_null());
|
||||||
|
assert!(text(&Value::Null, "token").is_err());
|
||||||
|
assert_eq!(text(&json!({"guest_id":"fixture-guest"}), "guest_id").unwrap(), "fixture-guest");
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn official_guest_failure_retry_schedule() {
|
||||||
|
let mut retry = RetryBackoff::default();
|
||||||
|
let actual: Vec<_> = (0..9).map(|_| retry.next().as_secs()).collect();
|
||||||
|
assert_eq!(actual, [15,30,45,60,75,90,99,99,99]);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn secure_temporary_code_format() {
|
||||||
|
for _ in 0..64 { let code = generate_code().unwrap(); assert_eq!(code.len(),8); assert!(code.bytes().all(|b| ALPHABET.contains(&b))); }
|
||||||
|
}
|
||||||
|
}
|
||||||
+76
-17
@@ -1,10 +1,13 @@
|
|||||||
//! Desktop sound for the viewer's 声音 menu: the default output's monitor (PulseAudio or
|
//! Desktop sound for the viewer's 声音 menu: the default output's monitor (PulseAudio or
|
||||||
//! PipeWire-pulse) is encoded to Opus by FFmpeg and sent on the negotiated audio m-line.
|
//! PipeWire-pulse) is encoded to Opus and sent on the negotiated audio m-line.
|
||||||
|
//! Encoding runs in this process with the `inproc-av` feature (unless `UU_FFMPEG_ROOT` is set),
|
||||||
|
//! otherwise an FFmpeg child process does it.
|
||||||
//! PipeWire monitors ignore the sink's mute by default, so 被控端静音运行 keeps the stream audible.
|
//! PipeWire monitors ignore the sink's mute by default, so 被控端静音运行 keeps the stream audible.
|
||||||
//! `UU_AUDIO=0` disables it; `UU_AUDIO_SOURCE` picks another Pulse source.
|
//! `UU_AUDIO=0` disables it; `UU_AUDIO_SOURCE` picks another Pulse source.
|
||||||
|
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::process::{Child, Command, Stdio};
|
use std::process::{Child, Command, Stdio};
|
||||||
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use webrtc::media::io::ogg_reader::OggReader;
|
use webrtc::media::io::ogg_reader::OggReader;
|
||||||
@@ -13,29 +16,34 @@ use webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState;
|
|||||||
use webrtc::peer_connection::RTCPeerConnection;
|
use webrtc::peer_connection::RTCPeerConnection;
|
||||||
use webrtc::track::track_local::track_local_static_sample::TrackLocalStaticSample;
|
use webrtc::track::track_local::track_local_static_sample::TrackLocalStaticSample;
|
||||||
|
|
||||||
/// One Opus packet per Ogg page (FFmpeg -page_duration), 20 ms each.
|
/// One Opus packet per 20 ms frame.
|
||||||
const FRAME: Duration = Duration::from_millis(20);
|
const FRAME: Duration = Duration::from_millis(20);
|
||||||
|
|
||||||
pub fn enabled() -> bool { std::env::var("UU_AUDIO").map_or(true, |v| v != "0") }
|
pub fn enabled() -> bool { std::env::var("UU_AUDIO").map_or(true, |v| v != "0") }
|
||||||
|
|
||||||
struct Encoder(Child);
|
/// Stops one running encoder when dropped: sets the in-process stop flag, or kills the FFmpeg child.
|
||||||
impl Drop for Encoder { fn drop(&mut self) { let _ = self.0.kill(); let _ = self.0.wait(); } }
|
struct Stopper { stop: Arc<AtomicBool>, child: Option<Child> }
|
||||||
|
impl Drop for Stopper {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.stop.store(true, Ordering::Relaxed);
|
||||||
|
if let Some(child) = self.child.as_mut() { let _ = child.kill(); let _ = child.wait(); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn spawn_encoder() -> Result<Encoder, String> {
|
fn spawn_encoder(source: &str) -> Result<Child, String> {
|
||||||
let source = std::env::var("UU_AUDIO_SOURCE").unwrap_or_else(|_| "@DEFAULT_MONITOR@".into());
|
|
||||||
let mut command = match crate::config::ffmpeg_root() {
|
let mut command = match crate::config::ffmpeg_root() {
|
||||||
Some(root) => { let mut c = Command::new(root.join("bin/ffmpeg")); c.env("LD_LIBRARY_PATH", root.join("lib/x86_64-linux-gnu")); c }
|
Some(root) => { let mut c = Command::new(root.join("bin/ffmpeg")); c.env("LD_LIBRARY_PATH", root.join("lib/x86_64-linux-gnu")); c }
|
||||||
None => Command::new("ffmpeg"),
|
None => Command::new("ffmpeg"),
|
||||||
};
|
};
|
||||||
command.args(["-hide_banner", "-loglevel", "error", "-nostdin",
|
command.args(["-hide_banner", "-loglevel", "error", "-nostdin",
|
||||||
"-f", "pulse", "-fragment_size", "1920", "-i", &source, "-vn",
|
"-f", "pulse", "-fragment_size", "1920", "-i", source, "-vn",
|
||||||
"-c:a", "libopus", "-ar", "48000", "-ac", "2", "-b:a", "128k", "-application", "lowdelay",
|
"-c:a", "libopus", "-ar", "48000", "-ac", "2", "-b:a", "128k", "-application", "lowdelay",
|
||||||
"-frame_duration", "20", "-page_duration", "20000", "-flush_packets", "1", "-f", "ogg", "pipe:1"])
|
"-frame_duration", "20", "-page_duration", "20000", "-flush_packets", "1", "-f", "ogg", "pipe:1"])
|
||||||
.stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::inherit());
|
.stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::inherit());
|
||||||
command.spawn().map(Encoder).map_err(|e| format!("Start audio encoder: {e}"))
|
command.spawn().map_err(|e| format!("Start audio encoder: {e}"))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads Opus packets until the encoder ends or `tx` closes. Blocking.
|
/// Reads Opus packets from the FFmpeg child's Ogg output until it ends or `tx` closes. Blocking.
|
||||||
fn read_packets(stdout: impl Read, tx: &tokio::sync::mpsc::Sender<bytes::Bytes>) -> Result<(), String> {
|
fn read_packets(stdout: impl Read, tx: &tokio::sync::mpsc::Sender<bytes::Bytes>) -> Result<(), String> {
|
||||||
let (mut reader, _) = OggReader::new(stdout, false).map_err(|e| format!("Audio Ogg header: {e}"))?;
|
let (mut reader, _) = OggReader::new(stdout, false).map_err(|e| format!("Audio Ogg header: {e}"))?;
|
||||||
loop {
|
loop {
|
||||||
@@ -45,6 +53,24 @@ fn read_packets(stdout: impl Read, tx: &tokio::sync::mpsc::Sender<bytes::Bytes>)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Starts one encoder feeding `tx`. The job ends with its result once the stopper is dropped or `tx` closes.
|
||||||
|
fn start(source: &str, tx: tokio::sync::mpsc::Sender<bytes::Bytes>)
|
||||||
|
-> Result<(Stopper, tokio::task::JoinHandle<Result<(), String>>), String> {
|
||||||
|
let stop = Arc::new(AtomicBool::new(false));
|
||||||
|
#[cfg(feature = "inproc-av")]
|
||||||
|
if crate::inproc_encoder::selected() {
|
||||||
|
let job = tokio::task::spawn_blocking({
|
||||||
|
let (stop, source) = (stop.clone(), source.to_owned());
|
||||||
|
move || crate::inproc_encoder::run_audio(&source, tx, stop)
|
||||||
|
});
|
||||||
|
return Ok((Stopper { stop, child: None }, job));
|
||||||
|
}
|
||||||
|
let mut child = spawn_encoder(source)?;
|
||||||
|
let stdout = child.stdout.take().ok_or("Encoder stdout missing")?;
|
||||||
|
let job = tokio::task::spawn_blocking(move || read_packets(stdout, &tx));
|
||||||
|
Ok((Stopper { stop, child: Some(child) }, job))
|
||||||
|
}
|
||||||
|
|
||||||
fn closed(pc: &RTCPeerConnection) -> bool {
|
fn closed(pc: &RTCPeerConnection) -> bool {
|
||||||
matches!(pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed)
|
matches!(pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed)
|
||||||
}
|
}
|
||||||
@@ -57,13 +83,12 @@ pub async fn stream_audio(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStati
|
|||||||
}
|
}
|
||||||
let mut failures = 0u32;
|
let mut failures = 0u32;
|
||||||
while !closed(&pc) {
|
while !closed(&pc) {
|
||||||
let mut encoder = match spawn_encoder() {
|
let source = crate::virtual_audio::source();
|
||||||
Ok(encoder) => encoder,
|
let (tx, mut rx) = tokio::sync::mpsc::channel::<bytes::Bytes>(25);
|
||||||
|
let (stopper, job) = match start(&source, tx) {
|
||||||
|
Ok(started) => started,
|
||||||
Err(e) => { log::warn!("{e}"); return; }
|
Err(e) => { log::warn!("{e}"); return; }
|
||||||
};
|
};
|
||||||
let Some(stdout) = encoder.0.stdout.take() else { return };
|
|
||||||
let (tx, mut rx) = tokio::sync::mpsc::channel::<bytes::Bytes>(25);
|
|
||||||
let reader = tokio::task::spawn_blocking(move || read_packets(stdout, &tx));
|
|
||||||
log::info!("Desktop audio started: opus 48k stereo");
|
log::info!("Desktop audio started: opus 48k stereo");
|
||||||
let mut packets = 0u64;
|
let mut packets = 0u64;
|
||||||
let mut health = tokio::time::interval(Duration::from_millis(500));
|
let mut health = tokio::time::interval(Duration::from_millis(500));
|
||||||
@@ -76,13 +101,15 @@ pub async fn stream_audio(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStati
|
|||||||
}
|
}
|
||||||
packets += 1;
|
packets += 1;
|
||||||
},
|
},
|
||||||
_ = health.tick() => if closed(&pc) { break },
|
_ = health.tick() => if closed(&pc) || crate::virtual_audio::source() != source { break },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Dropping the receiver and the stopper ends the encoder at its next packet.
|
||||||
drop(rx);
|
drop(rx);
|
||||||
drop(encoder); // kills FFmpeg, which ends the blocking reader
|
drop(stopper);
|
||||||
let result = reader.await.unwrap_or_else(|e| Err(e.to_string()));
|
let result = job.await.unwrap_or_else(|e| Err(e.to_string()));
|
||||||
if closed(&pc) { break; }
|
if closed(&pc) { break; }
|
||||||
|
if crate::virtual_audio::source() != source { failures=0; continue; }
|
||||||
log::warn!("Desktop audio stopped after {packets} packets: {:?}", result.err());
|
log::warn!("Desktop audio stopped after {packets} packets: {:?}", result.err());
|
||||||
failures = if packets > 250 { 0 } else { failures + 1 };
|
failures = if packets > 250 { 0 } else { failures + 1 };
|
||||||
if failures >= 5 { log::warn!("Desktop audio disabled for this connection"); return; }
|
if failures >= 5 { log::warn!("Desktop audio disabled for this connection"); return; }
|
||||||
@@ -90,3 +117,35 @@ pub async fn stream_audio(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStati
|
|||||||
}
|
}
|
||||||
log::info!("Desktop audio ended");
|
log::info!("Desktop audio ended");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Encodes the configured source for `seconds` and prints packet count, bitrate and frame spacing.
|
||||||
|
pub async fn probe(seconds: u64) -> Result<(), String> {
|
||||||
|
let source = std::env::var("UU_AUDIO_SOURCE").unwrap_or_else(|_| "@DEFAULT_MONITOR@".into());
|
||||||
|
let (tx, mut rx) = tokio::sync::mpsc::channel::<bytes::Bytes>(256);
|
||||||
|
let (stopper, job) = start(&source, tx)?;
|
||||||
|
let started = std::time::Instant::now();
|
||||||
|
let mut packets = 0u64;
|
||||||
|
let mut bytes = 0u64;
|
||||||
|
let mut max_gap = Duration::ZERO;
|
||||||
|
let mut last = None::<std::time::Instant>;
|
||||||
|
while started.elapsed() < Duration::from_secs(seconds) {
|
||||||
|
match tokio::time::timeout(Duration::from_secs(2), rx.recv()).await {
|
||||||
|
Ok(Some(data)) => {
|
||||||
|
let now = std::time::Instant::now();
|
||||||
|
if let Some(prev) = last { max_gap = max_gap.max(now - prev); }
|
||||||
|
last = Some(now);
|
||||||
|
packets += 1;
|
||||||
|
bytes += data.len() as u64;
|
||||||
|
}
|
||||||
|
_ => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let elapsed = started.elapsed().as_secs_f64();
|
||||||
|
drop(rx);
|
||||||
|
drop(stopper);
|
||||||
|
println!("Desktop audio probe: packets={packets} bytes={bytes} kbps={:.1} packets_per_s={:.1} max_gap_ms={}",
|
||||||
|
bytes as f64 * 8.0 / 1000.0 / elapsed, packets as f64 / elapsed, max_gap.as_millis());
|
||||||
|
// The encoder is stopped on purpose, so its end-of-stream error only matters if nothing arrived.
|
||||||
|
let result = job.await.map_err(|e| e.to_string())?;
|
||||||
|
if packets == 0 { result } else { Ok(()) }
|
||||||
|
}
|
||||||
+85
-5
@@ -73,7 +73,7 @@ pub struct ApiResponse<T> {
|
|||||||
pub data: Option<T>,
|
pub data: Option<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Clone, Deserialize)]
|
||||||
pub struct RoomData {
|
pub struct RoomData {
|
||||||
pub signaling_server: String,
|
pub signaling_server: String,
|
||||||
pub token: String,
|
pub token: String,
|
||||||
@@ -81,9 +81,14 @@ pub struct RoomData {
|
|||||||
pub ws_connect_timeout_ms: Option<u64>,
|
pub ws_connect_timeout_ms: Option<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Debug for RoomData {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str("RoomData { .. }") }
|
||||||
|
}
|
||||||
|
|
||||||
pub struct UUAuthClient {
|
pub struct UUAuthClient {
|
||||||
pub creds: Credentials,
|
pub creds: Credentials,
|
||||||
http: reqwest::Client,
|
http: reqwest::Client,
|
||||||
|
guest_id: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UUAuthClient {
|
impl UUAuthClient {
|
||||||
@@ -92,7 +97,16 @@ impl UUAuthClient {
|
|||||||
.timeout(std::time::Duration::from_secs(12))
|
.timeout(std::time::Duration::from_secs(12))
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
Self { creds, http }
|
Self { creds, http, guest_id: None }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Explicit guest identity: never inherits account Authorization/cookies.
|
||||||
|
/// Official ApiDefaultHeaders 0x1003853ac/0x1003853f8 (see assist evidence).
|
||||||
|
pub fn new_guest(mut device: Credentials, guest_id: String, token: String) -> Self {
|
||||||
|
device.user_id.clear(); device.csrf.clear(); device.token = token;
|
||||||
|
let mut client = Self::new(device);
|
||||||
|
client.guest_id = Some(guest_id);
|
||||||
|
client
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 计算网易 NRD HMAC-SHA256 签名
|
/// 计算网易 NRD HMAC-SHA256 签名
|
||||||
@@ -147,6 +161,7 @@ impl UUAuthClient {
|
|||||||
("x-param-system-id", &self.creds.system_id), ("x-param-user-id", &self.creds.user_id)] {
|
("x-param-system-id", &self.creds.system_id), ("x-param-user-id", &self.creds.user_id)] {
|
||||||
if !value.is_empty() { param_map.insert(key.into(), value.clone()); }
|
if !value.is_empty() { param_map.insert(key.into(), value.clone()); }
|
||||||
}
|
}
|
||||||
|
if let Some(id) = &self.guest_id { param_map.insert("x-param-guest-id".into(), id.clone()); }
|
||||||
param_map.insert("x-param-ts".into(), ts);
|
param_map.insert("x-param-ts".into(), ts);
|
||||||
|
|
||||||
let sign = self.sign_request(method, path, ¶m_map, body_str);
|
let sign = self.sign_request(method, path, ¶m_map, body_str);
|
||||||
@@ -160,10 +175,12 @@ impl UUAuthClient {
|
|||||||
);
|
);
|
||||||
if !self.creds.token.is_empty() {
|
if !self.creds.token.is_empty() {
|
||||||
if let Ok(v) = HeaderValue::from_str(&format!("Bearer {}", self.creds.token)) { headers.insert("Authorization", v); }
|
if let Ok(v) = HeaderValue::from_str(&format!("Bearer {}", self.creds.token)) { headers.insert("Authorization", v); }
|
||||||
if let Ok(v) = HeaderValue::from_str(&format!("nrd_access_token={}; nrd_csrf_access_token={}", self.creds.token, self.creds.csrf)) {
|
if self.guest_id.is_none() {
|
||||||
headers.insert("Cookie", v);
|
if let Ok(v) = HeaderValue::from_str(&format!("nrd_access_token={}; nrd_csrf_access_token={}", self.creds.token, self.creds.csrf)) {
|
||||||
|
headers.insert("Cookie", v);
|
||||||
|
}
|
||||||
|
if let Ok(v) = HeaderValue::from_str(&self.creds.csrf) { headers.insert("X-CSRF-TOKEN", v); }
|
||||||
}
|
}
|
||||||
if let Ok(v) = HeaderValue::from_str(&self.creds.csrf) { headers.insert("X-CSRF-TOKEN", v); }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (k, v) in param_map {
|
for (k, v) in param_map {
|
||||||
@@ -189,6 +206,34 @@ impl UUAuthClient {
|
|||||||
Ok((status, value))
|
Ok((status, value))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Guest APIs use requestPlain for empty requests; sign the actual empty body.
|
||||||
|
/// Errors intentionally exclude response text and URL (both can contain secrets).
|
||||||
|
pub async fn assist_post(&self, path: &str, body: Option<&serde_json::Value>) -> Result<(u16, serde_json::Value), String> {
|
||||||
|
self.assist_send(self.assist_request(path, body)?).await
|
||||||
|
}
|
||||||
|
/// Account share info is a GET (POST answers 405, verified live).
|
||||||
|
pub async fn assist_get(&self, path: &str) -> Result<(u16, serde_json::Value), String> {
|
||||||
|
let request = self.http.get(format!("{BASE_URL}{path}")).headers(self.build_headers("GET", path, "")).build()
|
||||||
|
.map_err(|_| "assist request construction failed")?;
|
||||||
|
self.assist_send(request).await
|
||||||
|
}
|
||||||
|
async fn assist_send(&self, request: reqwest::Request) -> Result<(u16, serde_json::Value), String> {
|
||||||
|
let response = self.http.execute(request)
|
||||||
|
.await.map_err(|_| "assist request transport failed")?;
|
||||||
|
let status = response.status().as_u16();
|
||||||
|
let bytes = response.bytes().await.map_err(|_| "assist response read failed")?;
|
||||||
|
if bytes.len() > 65536 { return Err("assist response too large".into()); }
|
||||||
|
let value = serde_json::from_slice(&bytes).map_err(|_| "assist response malformed")?;
|
||||||
|
Ok((status, value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn assist_request(&self, path: &str, body: Option<&serde_json::Value>) -> Result<reqwest::Request, String> {
|
||||||
|
let body = body.map(serde_json::to_string).transpose().map_err(|_| "assist request encoding failed")?.unwrap_or_default();
|
||||||
|
self.http.post(format!("{BASE_URL}{path}"))
|
||||||
|
.headers(self.build_headers("POST", path, &body)).body(body).build()
|
||||||
|
.map_err(|_| "assist request construction failed".into())
|
||||||
|
}
|
||||||
|
|
||||||
/// 向网易服务器请求创建房间,获取最新的信令服务器与鉴权 Token
|
/// 向网易服务器请求创建房间,获取最新的信令服务器与鉴权 Token
|
||||||
pub async fn create_room(&self) -> Result<RoomData, RoomError> {
|
pub async fn create_room(&self) -> Result<RoomData, RoomError> {
|
||||||
let (status, body) = self.post_json("/api/v1/room/create", &serde_json::json!({})).await.map_err(RoomError::Other)?;
|
let (status, body) = self.post_json("/api/v1/room/create", &serde_json::json!({})).await.map_err(RoomError::Other)?;
|
||||||
@@ -231,3 +276,38 @@ mod room_lifetime_tests {
|
|||||||
assert_eq!(room_token_lifetime("h.invalid.s",1000),None);
|
assert_eq!(room_token_lifetime("h.invalid.s",1000),None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod guest_tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn official_request_plain_body_and_sanitized_room_debug() {
|
||||||
|
let client = UUAuthClient::new_guest(Credentials { sign_key: "fixture-key".into(), ..Default::default() },
|
||||||
|
"fixture-guest".into(), "fixture-token".into());
|
||||||
|
let request = client.assist_request("/api/v1/guest/create", None).unwrap();
|
||||||
|
assert_eq!(request.method(), reqwest::Method::POST);
|
||||||
|
assert_eq!(request.body().unwrap().as_bytes(), Some(&b""[..]));
|
||||||
|
let other = client.assist_request("/api/v1/guest/create", Some(&serde_json::json!({}))).unwrap();
|
||||||
|
assert_eq!(other.body().unwrap().as_bytes(), Some(&b"{}"[..]));
|
||||||
|
assert_ne!(request.headers()["x-param-sign"], other.headers()["x-param-sign"]);
|
||||||
|
let room: RoomData = serde_json::from_value(serde_json::json!({"signaling_server":"wss://fixture.invalid", "token":"synthetic-room-secret"})).unwrap();
|
||||||
|
assert!(!format!("{room:?}").contains("synthetic-room-secret"));
|
||||||
|
assert!(serde_json::from_value::<RoomData>(serde_json::json!({"token":42})).is_err());
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn guest_signs_guest_id_and_never_inherits_account_identity() {
|
||||||
|
let account = Credentials { user_id: "fixture-account".into(), token: "fixture-account-token".into(),
|
||||||
|
csrf: "fixture-account-csrf".into(), client_id: "fixture-client".into(),
|
||||||
|
device_id: "fixture-device".into(), sign_key: "fixture-sign-key".into(), ..Default::default() };
|
||||||
|
let client = UUAuthClient::new_guest(account, "fixture-guest".into(), "fixture-guest-token".into());
|
||||||
|
let headers = client.build_headers("POST", "/api/v1/guest/room/create", "");
|
||||||
|
assert_eq!(headers["authorization"], "Bearer fixture-guest-token");
|
||||||
|
assert_eq!(headers["x-param-guest-id"], "fixture-guest");
|
||||||
|
for key in ["x-param-user-id", "cookie", "x-csrf-token"] { assert!(!headers.contains_key(key)); }
|
||||||
|
let params: BTreeMap<String,String> = headers.iter().filter_map(|(key, value)|
|
||||||
|
key.as_str().starts_with("x-param-").then(|| (key.to_string(), value.to_str().unwrap().to_owned()))).collect();
|
||||||
|
assert_eq!(headers["x-param-sign"], client.sign_request("POST", "/api/v1/guest/room/create", ¶ms, ""));
|
||||||
|
let mut without_guest = params; without_guest.remove("x-param-guest-id");
|
||||||
|
assert_ne!(headers["x-param-sign"], client.sign_request("POST", "/api/v1/guest/room/create", &without_guest, ""));
|
||||||
|
}
|
||||||
|
}
|
||||||
+13
-3
@@ -46,6 +46,7 @@ pub struct ScreenCapture {
|
|||||||
pub init_error: Option<String>,
|
pub init_error: Option<String>,
|
||||||
pub portal: Option<std::sync::Arc<crate::portal::PortalSession>>,
|
pub portal: Option<std::sync::Arc<crate::portal::PortalSession>>,
|
||||||
pub display: String,
|
pub display: String,
|
||||||
|
kms: Option<crate::kms::Capture>,
|
||||||
pub width: u32,
|
pub width: u32,
|
||||||
pub height: u32,
|
pub height: u32,
|
||||||
buffer: Mutex<Vec<u8>>,
|
buffer: Mutex<Vec<u8>>,
|
||||||
@@ -54,15 +55,21 @@ pub struct ScreenCapture {
|
|||||||
impl ScreenCapture {
|
impl ScreenCapture {
|
||||||
pub fn new() -> Result<Self, String> {
|
pub fn new() -> Result<Self, String> {
|
||||||
let backend=crate::desktop_backend::detect()?;
|
let backend=crate::desktop_backend::detect()?;
|
||||||
|
if backend == crate::desktop_backend::DesktopBackend::Kms {
|
||||||
|
let kms=crate::kms::Capture::open()?;
|
||||||
|
let (width,height)=(kms.width,kms.height);
|
||||||
|
return Ok(Self { backend,init_error:None,portal:None,display:"kms:linear".into(),width,height,
|
||||||
|
buffer:Mutex::new(vec![0;width as usize*height as usize*4]),kms:Some(kms) });
|
||||||
|
}
|
||||||
if backend == crate::desktop_backend::DesktopBackend::Portal {
|
if backend == crate::desktop_backend::DesktopBackend::Portal {
|
||||||
let session=std::sync::Arc::new(crate::portal::PortalSession::open()?);
|
let session=std::sync::Arc::new(crate::portal::PortalSession::open()?);
|
||||||
let (width,height)=(session.width,session.height);
|
let (width,height)=(session.width,session.height);
|
||||||
return Ok(Self { backend,init_error:None,portal:Some(session),display:"wayland:portal".into(),width,height,
|
return Ok(Self { backend,init_error:None,portal:Some(session),kms:None,display:"wayland:portal".into(),width,height,
|
||||||
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
|
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
|
||||||
}
|
}
|
||||||
if backend == crate::desktop_backend::DesktopBackend::Wayland {
|
if backend == crate::desktop_backend::DesktopBackend::Wayland {
|
||||||
let (width,height)=crate::wayland::init()?;
|
let (width,height)=crate::wayland::init()?;
|
||||||
return Ok(Self { backend, init_error:None, portal:None, display:"wayland:native".into(), width,height,
|
return Ok(Self { backend, init_error:None, portal:None, kms:None, display:"wayland:native".into(), width,height,
|
||||||
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
|
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
|
||||||
}
|
}
|
||||||
let ret = unsafe { x11_init(std::ptr::null()) };
|
let ret = unsafe { x11_init(std::ptr::null()) };
|
||||||
@@ -90,6 +97,7 @@ impl ScreenCapture {
|
|||||||
backend,
|
backend,
|
||||||
init_error:None,
|
init_error:None,
|
||||||
portal:None,
|
portal:None,
|
||||||
|
kms:None,
|
||||||
display,
|
display,
|
||||||
width: w as u32,
|
width: w as u32,
|
||||||
height: h as u32,
|
height: h as u32,
|
||||||
@@ -100,13 +108,14 @@ impl ScreenCapture {
|
|||||||
/// Metadata for non-desktop tools. Never advertise a usable fake screen.
|
/// Metadata for non-desktop tools. Never advertise a usable fake screen.
|
||||||
pub fn unavailable(error:String)->Self {
|
pub fn unavailable(error:String)->Self {
|
||||||
Self { backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11),
|
Self { backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11),
|
||||||
init_error:Some(error), portal:None, display:String::new(), width:0,height:0,buffer:Mutex::new(Vec::new()) }
|
init_error:Some(error), portal:None, kms:None, display:String::new(), width:0,height:0,buffer:Mutex::new(Vec::new()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 截取一帧 BGRA 图像
|
/// 截取一帧 BGRA 图像
|
||||||
pub fn capture_frame(&self) -> Result<Vec<u8>, String> {
|
pub fn capture_frame(&self) -> Result<Vec<u8>, String> {
|
||||||
if let Some(error)=&self.init_error { return Err(error.clone()); }
|
if let Some(error)=&self.init_error { return Err(error.clone()); }
|
||||||
let mut buf = self.buffer.lock().unwrap();
|
let mut buf = self.buffer.lock().unwrap();
|
||||||
|
if let Some(kms)=&self.kms { kms.capture(&mut buf)?; return Ok(buf.clone()); }
|
||||||
if let Some(session)=&self.portal { session.capture(&mut buf)?; return Ok(buf.clone()); }
|
if let Some(session)=&self.portal { session.capture(&mut buf)?; return Ok(buf.clone()); }
|
||||||
if self.backend == crate::desktop_backend::DesktopBackend::Wayland {
|
if self.backend == crate::desktop_backend::DesktopBackend::Wayland {
|
||||||
crate::wayland::capture(&mut buf)?;
|
crate::wayland::capture(&mut buf)?;
|
||||||
@@ -132,6 +141,7 @@ impl Drop for ScreenCapture {
|
|||||||
|
|
||||||
/// Keep the display visible only while a peer is streaming; never unlock it.
|
/// Keep the display visible only while a peer is streaming; never unlock it.
|
||||||
pub fn keep_display_awake() -> Result<(), String> {
|
pub fn keep_display_awake() -> Result<(), String> {
|
||||||
|
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Kms { return Ok(()); }
|
||||||
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Portal { return Ok(()); }
|
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Portal { return Ok(()); }
|
||||||
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Wayland {
|
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Wayland {
|
||||||
return crate::wayland::wake();
|
return crate::wayland::wake();
|
||||||
|
|||||||
+1
-1
@@ -20,7 +20,7 @@ extern "C" {
|
|||||||
|
|
||||||
pub fn start(display: String) -> ClipboardBridge {
|
pub fn start(display: String) -> ClipboardBridge {
|
||||||
if display == "wayland:native" { return start_wayland(); }
|
if display == "wayland:native" { return start_wayland(); }
|
||||||
if display == "wayland:portal" {
|
if display == "wayland:portal" || display.starts_with("kms:") {
|
||||||
let (incoming,_)=watch::channel(None);let (_,outgoing)=watch::channel(None);
|
let (incoming,_)=watch::channel(None);let (_,outgoing)=watch::channel(None);
|
||||||
let (sender,ready)=tokio::sync::oneshot::channel();let _=sender.send(false);
|
let (sender,ready)=tokio::sync::oneshot::channel();let _=sender.send(false);
|
||||||
return ClipboardBridge {incoming,outgoing,ready};
|
return ClipboardBridge {incoming,outgoing,ready};
|
||||||
|
|||||||
+21
-16
@@ -4,9 +4,13 @@ pub fn run() -> Result<(),String> {
|
|||||||
if std::fs::read_to_string("/proc/sys/kernel/hostname").unwrap_or_default().trim()!="uu-hyprland-test" {
|
if std::fs::read_to_string("/proc/sys/kernel/hostname").unwrap_or_default().trim()!="uu-hyprland-test" {
|
||||||
return Err("Clipboard acceptance is restricted to the isolated VM".into());
|
return Err("Clipboard acceptance is restricted to the isolated VM".into());
|
||||||
}
|
}
|
||||||
let session=Arc::new(crate::portal::PortalSession::open()?);
|
let screen=crate::capture::ScreenCapture::new()?;
|
||||||
let bridge=crate::clipboard::start_portal(session.clone());
|
let input=Arc::new(crate::input::InputInjector::new());
|
||||||
if !bridge.ready.blocking_recv().unwrap_or(false) { return Err("Portal clipboard permission not granted".into()); }
|
input.bind_portal(screen.portal.clone());
|
||||||
|
let pointer=input.start_pointer_worker();
|
||||||
|
let bridge=if let Some(session)=screen.portal.clone() { crate::clipboard::start_portal(session) }
|
||||||
|
else { crate::clipboard::start(screen.display.clone()) };
|
||||||
|
if !bridge.ready.blocking_recv().unwrap_or(false) { return Err("Desktop clipboard permission not granted".into()); }
|
||||||
let mut child=Command::new("python3").arg("/home/uuremote/clipboard-acceptance.py")
|
let mut child=Command::new("python3").arg("/home/uuremote/clipboard-acceptance.py")
|
||||||
.env("GDK_BACKEND","wayland").env("GSK_RENDERER","cairo")
|
.env("GDK_BACKEND","wayland").env("GSK_RENDERER","cairo")
|
||||||
.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::null()).spawn().map_err(|_|"Start GTK clipboard fixture")?;
|
.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::null()).spawn().map_err(|_|"Start GTK clipboard fixture")?;
|
||||||
@@ -29,13 +33,14 @@ pub fn run() -> Result<(),String> {
|
|||||||
bridge.incoming.send_replace(Some(text.clone()));
|
bridge.incoming.send_replace(Some(text.clone()));
|
||||||
let deadline=Instant::now()+Duration::from_secs(6);let mut matched=false;
|
let deadline=Instant::now()+Duration::from_secs(6);let mut matched=false;
|
||||||
while Instant::now()<deadline { if check("read",&text)? {matched=true;break;} std::thread::sleep(Duration::from_millis(100)); }
|
while Instant::now()<deadline { if check("read",&text)? {matched=true;break;} std::thread::sleep(Duration::from_millis(100)); }
|
||||||
if !matched { return Err(format!("Portal clipboard incoming {name} mismatch")); }
|
if !matched { return Err(format!("Desktop clipboard incoming {name} mismatch")); }
|
||||||
std::thread::sleep(Duration::from_millis(300));
|
std::thread::sleep(Duration::from_millis(300));
|
||||||
if outgoing.has_changed().unwrap_or(false) { return Err("Portal clipboard echoed its own selection".into()); }
|
if outgoing.has_changed().unwrap_or(false) { return Err("Desktop clipboard echoed its own selection".into()); }
|
||||||
println!("Portal clipboard incoming {name}: passed; no echo");
|
println!("Desktop clipboard incoming {name}: passed; no echo");
|
||||||
}
|
}
|
||||||
session.move_pointer(f64::from(session.width)/2.0,f64::from(session.height)/2.0,false)?;
|
input.mouse_absolute_normalized(0.5,0.5,(screen.width,screen.height));
|
||||||
session.button(1,true)?; session.button(1,false)?;
|
std::thread::sleep(Duration::from_millis(100));
|
||||||
|
input.mouse_button(1,true); input.mouse_button(1,false);
|
||||||
std::thread::sleep(Duration::from_millis(300));
|
std::thread::sleep(Duration::from_millis(300));
|
||||||
let external="UU outgoing 中文🚀\nsecond line";
|
let external="UU outgoing 中文🚀\nsecond line";
|
||||||
if !check("set",external)? { return Err("GTK clipboard ownership failed".into()); }
|
if !check("set",external)? { return Err("GTK clipboard ownership failed".into()); }
|
||||||
@@ -44,25 +49,25 @@ pub fn run() -> Result<(),String> {
|
|||||||
if outgoing.has_changed().unwrap_or(false) && outgoing.borrow_and_update().as_deref()==Some(external) {matched=true;break;}
|
if outgoing.has_changed().unwrap_or(false) && outgoing.borrow_and_update().as_deref()==Some(external) {matched=true;break;}
|
||||||
std::thread::sleep(Duration::from_millis(100));
|
std::thread::sleep(Duration::from_millis(100));
|
||||||
}
|
}
|
||||||
if !matched { return Err("Portal clipboard outgoing Unicode mismatch".into()); }
|
if !matched { return Err("Desktop clipboard outgoing Unicode mismatch".into()); }
|
||||||
println!("Portal clipboard outgoing unicode: passed");
|
println!("Desktop clipboard outgoing unicode: passed");
|
||||||
bridge.incoming.send_replace(Some("x".repeat(crate::clipboard::MAX_TEXT+1)));
|
bridge.incoming.send_replace(Some("x".repeat(crate::clipboard::MAX_TEXT+1)));
|
||||||
std::thread::sleep(Duration::from_millis(400));
|
std::thread::sleep(Duration::from_millis(400));
|
||||||
if !check("read",external)? { return Err("Oversized text altered native clipboard".into()); }
|
if !check("read",external)? { return Err("Oversized text altered native clipboard".into()); }
|
||||||
println!("Portal clipboard incoming oversized rejection: passed");
|
println!("Desktop clipboard incoming oversized rejection: passed");
|
||||||
if !check("set",&"y".repeat(crate::clipboard::MAX_TEXT+1))? { return Err("GTK oversized fixture setup failed".into()); }
|
if !check("set",&"y".repeat(crate::clipboard::MAX_TEXT+1))? { return Err("GTK oversized fixture setup failed".into()); }
|
||||||
std::thread::sleep(Duration::from_secs(3));
|
std::thread::sleep(Duration::from_secs(3));
|
||||||
if outgoing.has_changed().unwrap_or(false) { return Err("Portal clipboard published oversized native text".into()); }
|
if outgoing.has_changed().unwrap_or(false) { return Err("Desktop clipboard published oversized native text".into()); }
|
||||||
println!("Portal clipboard outgoing oversized rejection: passed");
|
println!("Desktop clipboard outgoing oversized rejection: passed");
|
||||||
if !check("set","")? { return Err("GTK empty fixture setup failed".into()); }
|
if !check("set","")? { return Err("GTK empty fixture setup failed".into()); }
|
||||||
let deadline=Instant::now()+Duration::from_secs(6);let mut matched=false;
|
let deadline=Instant::now()+Duration::from_secs(6);let mut matched=false;
|
||||||
while Instant::now()<deadline {
|
while Instant::now()<deadline {
|
||||||
if outgoing.has_changed().unwrap_or(false) && outgoing.borrow_and_update().as_deref()==Some("") {matched=true;break;}
|
if outgoing.has_changed().unwrap_or(false) && outgoing.borrow_and_update().as_deref()==Some("") {matched=true;break;}
|
||||||
std::thread::sleep(Duration::from_millis(100));
|
std::thread::sleep(Duration::from_millis(100));
|
||||||
}
|
}
|
||||||
if !matched { return Err("Portal clipboard outgoing empty text mismatch".into()); }
|
if !matched { return Err("Desktop clipboard outgoing empty text mismatch".into()); }
|
||||||
println!("Portal clipboard outgoing empty: passed");
|
println!("Desktop clipboard outgoing empty: passed");
|
||||||
drop(writer); Ok(())
|
drop(writer); Ok(())
|
||||||
})();
|
})();
|
||||||
let _=child.kill();let _=child.wait();result
|
let _=child.kill();let _=child.wait();pointer.abort();input.release_held();result
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
//! Damage-driven X11 capture for the in-process encoder: XDamage reports when the root window changed,
|
||||||
|
//! XShm copies it out as 32-bit BGRX. Pure Rust over x11rb; only the SysV shared-memory calls use libc.
|
||||||
|
use std::os::unix::io::AsRawFd;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
use x11rb::connection::{Connection, RequestConnection};
|
||||||
|
use x11rb::protocol::damage::{self, ConnectionExt as _, ReportLevel};
|
||||||
|
use x11rb::protocol::shm::{self, ConnectionExt as _};
|
||||||
|
use x11rb::protocol::xproto::Window;
|
||||||
|
use x11rb::rust_connection::RustConnection;
|
||||||
|
use x11rb::NONE;
|
||||||
|
|
||||||
|
/// X11 ZPixmap image format.
|
||||||
|
const Z_PIXMAP: u8 = 2;
|
||||||
|
|
||||||
|
pub struct DamageCapture {
|
||||||
|
conn: RustConnection,
|
||||||
|
root: Window,
|
||||||
|
damage: damage::Damage,
|
||||||
|
damage_event: u8,
|
||||||
|
width: u16,
|
||||||
|
height: u16,
|
||||||
|
seg: shm::Seg,
|
||||||
|
shm_ptr: *mut u8,
|
||||||
|
len: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn text(error: impl std::fmt::Display) -> String { error.to_string() }
|
||||||
|
|
||||||
|
impl DamageCapture {
|
||||||
|
/// Connects to `display` and sets up XDamage and XShm. Errors name the missing extension or failed step.
|
||||||
|
pub fn open(display: &str) -> Result<Self, String> {
|
||||||
|
let (conn, screen_num) = RustConnection::connect(Some(display)).map_err(text)?;
|
||||||
|
let damage_info = conn.extension_information(damage::X11_EXTENSION_NAME).map_err(text)?
|
||||||
|
.ok_or("XDamage extension is missing")?;
|
||||||
|
if conn.extension_information(shm::X11_EXTENSION_NAME).map_err(text)?.is_none() {
|
||||||
|
return Err("XShm extension is missing".into());
|
||||||
|
}
|
||||||
|
conn.damage_query_version(1, 1).map_err(text)?.reply().map_err(text)?;
|
||||||
|
|
||||||
|
let setup = conn.setup();
|
||||||
|
let screen = setup.roots.get(screen_num).ok_or("X screen not found")?;
|
||||||
|
let (root, width, height, depth) = (screen.root, screen.width_in_pixels, screen.height_in_pixels, screen.root_depth);
|
||||||
|
// The frame layout assumes packed 32-bit pixels; anything else would shear the picture.
|
||||||
|
let format = setup.pixmap_formats.iter().find(|f| f.depth == depth).ok_or("no pixmap format for the root depth")?;
|
||||||
|
if format.bits_per_pixel != 32 { return Err(format!("root depth {depth} is not 32 bits per pixel")); }
|
||||||
|
let len = width as usize * height as usize * 4;
|
||||||
|
|
||||||
|
// NonEmpty: one event each time the root goes from clean to damaged; re-armed in `wait`.
|
||||||
|
let damage = conn.generate_id().map_err(text)?;
|
||||||
|
conn.damage_create(damage, root, ReportLevel::NON_EMPTY).map_err(text)?.check().map_err(text)?;
|
||||||
|
|
||||||
|
// Nothing below can fail without also releasing the local mapping, so every error path is covered here.
|
||||||
|
let seg = conn.generate_id().map_err(text)?;
|
||||||
|
let shmid = unsafe { libc::shmget(libc::IPC_PRIVATE, len, libc::IPC_CREAT | 0o600) };
|
||||||
|
if shmid < 0 { return Err("shmget failed".into()); }
|
||||||
|
let shm_ptr = unsafe { libc::shmat(shmid, std::ptr::null(), 0) } as *mut u8;
|
||||||
|
if shm_ptr as isize == -1 {
|
||||||
|
unsafe { libc::shmctl(shmid, libc::IPC_RMID, std::ptr::null_mut()) };
|
||||||
|
return Err("shmat failed".into());
|
||||||
|
}
|
||||||
|
let attach = conn.shm_attach(seg, shmid as u32, false).map_err(text).and_then(|c| c.check().map_err(text));
|
||||||
|
// Marked for removal either way; the kernel frees it after the last detach.
|
||||||
|
unsafe { libc::shmctl(shmid, libc::IPC_RMID, std::ptr::null_mut()) };
|
||||||
|
if let Err(error) = attach {
|
||||||
|
unsafe { libc::shmdt(shm_ptr as *const libc::c_void) };
|
||||||
|
return Err(error);
|
||||||
|
}
|
||||||
|
Ok(Self { conn, root, damage, damage_event: damage_info.first_event, width, height, seg, shm_ptr, len })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn size(&self) -> (u32, u32) { (self.width as u32, self.height as u32) }
|
||||||
|
|
||||||
|
/// Waits up to `timeout` for the screen to change. Returns `true` on change, `false` on timeout.
|
||||||
|
pub fn wait(&mut self, timeout: Duration) -> Result<bool, String> {
|
||||||
|
let deadline = Instant::now() + timeout;
|
||||||
|
loop {
|
||||||
|
let mut changed = false;
|
||||||
|
while let Some(event) = self.conn.poll_for_raw_event().map_err(text)? {
|
||||||
|
// XDamageNotify is the extension's first event; the wire code keeps the high bit for synthetic events.
|
||||||
|
if event[0] & 0x7f == self.damage_event { changed = true; }
|
||||||
|
}
|
||||||
|
if changed {
|
||||||
|
self.conn.damage_subtract(self.damage, NONE, NONE).map_err(text)?;
|
||||||
|
self.conn.flush().map_err(text)?;
|
||||||
|
return Ok(true);
|
||||||
|
}
|
||||||
|
let remaining = deadline.saturating_duration_since(Instant::now());
|
||||||
|
if remaining.is_zero() { return Ok(false); }
|
||||||
|
let mut pfd = libc::pollfd { fd: self.conn.stream().as_raw_fd(), events: libc::POLLIN, revents: 0 };
|
||||||
|
match unsafe { libc::poll(&mut pfd, 1, remaining.as_millis().max(1) as i32) } {
|
||||||
|
ready if ready < 0 => return Err("poll on the X connection failed".into()),
|
||||||
|
// Readable but not yet a whole event: poll_for_raw_event would return None and poll would
|
||||||
|
// fire again at once, spinning a core. Block for the event instead.
|
||||||
|
ready if ready > 0 => {
|
||||||
|
let event = self.conn.wait_for_raw_event().map_err(text)?;
|
||||||
|
if event[0] & 0x7f == self.damage_event { changed = true; }
|
||||||
|
if changed {
|
||||||
|
self.conn.damage_subtract(self.damage, NONE, NONE).map_err(text)?;
|
||||||
|
self.conn.flush().map_err(text)?;
|
||||||
|
return Ok(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Copies the root into the shared segment and returns it as rows of `width * 4` bytes.
|
||||||
|
/// The slice is valid until the next call.
|
||||||
|
pub fn capture(&mut self) -> Result<&[u8], String> {
|
||||||
|
self.conn.shm_get_image(self.root, 0, 0, self.width, self.height, !0, Z_PIXMAP, self.seg, 0)
|
||||||
|
.map_err(text)?.reply().map_err(text)?;
|
||||||
|
Ok(unsafe { std::slice::from_raw_parts(self.shm_ptr, self.len) })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for DamageCapture {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let _ = self.conn.damage_destroy(self.damage);
|
||||||
|
let _ = self.conn.shm_detach(self.seg);
|
||||||
|
let _ = self.conn.flush();
|
||||||
|
unsafe { libc::shmdt(self.shm_ptr as *const libc::c_void) };
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,10 @@
|
|||||||
//! Select the host desktop, not the presence of an XWayland DISPLAY socket.
|
//! Select the host desktop, not the presence of an XWayland DISPLAY socket.
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
pub enum DesktopBackend { X11, Wayland, Portal }
|
pub enum DesktopBackend { X11, Wayland, Portal, Kms }
|
||||||
|
|
||||||
|
/// `UU_DESKTOP_BACKEND=none`: headless host — no display is opened; terminal, file transfer and
|
||||||
|
/// port mapping stay available (servers without a monitor or logged-in session).
|
||||||
|
pub fn headless() -> bool { std::env::var("UU_DESKTOP_BACKEND").is_ok_and(|v| v == "none") }
|
||||||
|
|
||||||
pub fn detect() -> Result<DesktopBackend, String> {
|
pub fn detect() -> Result<DesktopBackend, String> {
|
||||||
let backend=select(std::env::var("UU_DESKTOP_BACKEND").ok().as_deref(),
|
let backend=select(std::env::var("UU_DESKTOP_BACKEND").ok().as_deref(),
|
||||||
@@ -15,13 +19,15 @@ fn select(explicit: Option<&str>, session: Option<&str>, socket: Option<&str>) -
|
|||||||
match explicit {
|
match explicit {
|
||||||
Some("x11") => Ok(DesktopBackend::X11),
|
Some("x11") => Ok(DesktopBackend::X11),
|
||||||
Some("wayland") => Ok(DesktopBackend::Wayland),
|
Some("wayland") => Ok(DesktopBackend::Wayland),
|
||||||
|
Some("kms") => Ok(DesktopBackend::Kms),
|
||||||
Some("portal") => Ok(DesktopBackend::Portal),
|
Some("portal") => Ok(DesktopBackend::Portal),
|
||||||
|
Some("none") => Ok(DesktopBackend::X11), // never opened, see headless()
|
||||||
None | Some("auto") => {
|
None | Some("auto") => {
|
||||||
if session == Some("wayland") || socket.is_some_and(|s| !s.is_empty()) {
|
if session == Some("wayland") || socket.is_some_and(|s| !s.is_empty()) {
|
||||||
Ok(DesktopBackend::Wayland)
|
Ok(DesktopBackend::Wayland)
|
||||||
} else { Ok(DesktopBackend::X11) }
|
} else { Ok(DesktopBackend::X11) }
|
||||||
},
|
},
|
||||||
Some(_) => Err("UU_DESKTOP_BACKEND must be auto, x11, wayland or portal".into()),
|
Some(_) => Err("UU_DESKTOP_BACKEND must be auto, x11, wayland, portal, kms or none".into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,6 +40,7 @@ mod tests {
|
|||||||
assert_eq!(select(None,None,Some("wayland-1")),Ok(DesktopBackend::Wayland));
|
assert_eq!(select(None,None,Some("wayland-1")),Ok(DesktopBackend::Wayland));
|
||||||
assert_eq!(select(None,Some("x11"),None),Ok(DesktopBackend::X11));
|
assert_eq!(select(None,Some("x11"),None),Ok(DesktopBackend::X11));
|
||||||
assert_eq!(select(Some("x11"),Some("wayland"),Some("wayland-0")),Ok(DesktopBackend::X11));
|
assert_eq!(select(Some("x11"),Some("wayland"),Some("wayland-0")),Ok(DesktopBackend::X11));
|
||||||
|
assert_eq!(select(Some("kms"),Some("wayland"),Some("wayland-0")),Ok(DesktopBackend::Kms));
|
||||||
assert!(select(Some("typo"),None,None).is_err());
|
assert!(select(Some("typo"),None,None).is_err());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,397 @@
|
|||||||
|
//! In-process X11 capture and H.264 encoding through libavdevice/libavcodec.
|
||||||
|
//! It replaces the `ffmpeg` child process with the same codec options, so the viewer
|
||||||
|
//! still receives the same Annex-B access units (AUD, SPS/PPS, IDR, P).
|
||||||
|
use std::ffi::CString;
|
||||||
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
use std::sync::{Arc, Once};
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
use ffmpeg_next as ff;
|
||||||
|
use ff::format::{format::Format, sample, Pixel, Sample};
|
||||||
|
use ff::ChannelLayout;
|
||||||
|
use ff::util::picture;
|
||||||
|
use ff::{codec, encoder, frame, software::scaling, Dictionary, Packet, Rational};
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
|
use crate::damage_capture::DamageCapture;
|
||||||
|
|
||||||
|
pub struct Config {
|
||||||
|
pub display: String,
|
||||||
|
pub input: (u32, u32),
|
||||||
|
pub output: Option<(u32, u32)>,
|
||||||
|
pub fps: u32,
|
||||||
|
pub gop: u32,
|
||||||
|
pub cq: u32,
|
||||||
|
pub target_bps: i64,
|
||||||
|
pub max_bps: i64,
|
||||||
|
pub buffer_bits: i64,
|
||||||
|
pub encoder: crate::video::Encoder,
|
||||||
|
/// Capture on XDamage changes (XShm) instead of x11grab; `UU_CAPTURE=grab` turns it off.
|
||||||
|
pub damage: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `UU_VIDEO_PIPELINE=process` keeps the original ffmpeg child-process path.
|
||||||
|
pub fn disabled() -> bool {
|
||||||
|
std::env::var("UU_VIDEO_PIPELINE").is_ok_and(|v| v == "process")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Damage-driven capture is on unless `UU_CAPTURE=grab` asks for x11grab.
|
||||||
|
pub fn damage_enabled() -> bool {
|
||||||
|
std::env::var("UU_CAPTURE").map_or(true, |v| v != "grab")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// In-process capture is used unless the process path is requested, or a bundled FFmpeg prefix
|
||||||
|
/// (`UU_FFMPEG_ROOT` / config `ffmpeg_root`) is set: that build was chosen on purpose, so keep using it.
|
||||||
|
pub fn selected() -> bool {
|
||||||
|
!disabled() && crate::config::ffmpeg_root().is_none()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn text(error: ff::Error) -> String { error.to_string() }
|
||||||
|
|
||||||
|
fn ensure_init() -> Result<(), String> {
|
||||||
|
static INIT: Once = Once::new();
|
||||||
|
let mut result = Ok(());
|
||||||
|
INIT.call_once(|| {
|
||||||
|
if let Err(error) = ff::init() { result = Err(text(error)); }
|
||||||
|
ff::device::register_all();
|
||||||
|
});
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends one access unit per encoder packet. Returns `Ok(false)` when the receiver is gone.
|
||||||
|
fn drain(encoder: &mut encoder::Video, packet: &mut Packet, units: &mpsc::Sender<Vec<u8>>) -> Result<bool, String> {
|
||||||
|
loop {
|
||||||
|
match encoder.receive_packet(packet) {
|
||||||
|
Ok(()) => {
|
||||||
|
let data = packet.data().unwrap_or_default().to_vec();
|
||||||
|
if units.blocking_send(data).is_err() { return Ok(false); }
|
||||||
|
}
|
||||||
|
Err(ff::Error::Other { errno }) if errno == libc::EAGAIN => return Ok(true),
|
||||||
|
Err(error) => return Err(text(error)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Opens the H.264 encoder for `config`. Returns the encoder, its input pixel format and output size.
|
||||||
|
fn open_encoder(config: &Config) -> Result<(encoder::Video, Pixel, u32, u32), String> {
|
||||||
|
let (width, height) = config.output.unwrap_or(config.input);
|
||||||
|
let codec = encoder::find_by_name(config.encoder.name())
|
||||||
|
.ok_or("H.264 encoder is not available in this FFmpeg build")?;
|
||||||
|
// Allocate for this codec so FFmpeg fills its defaults; libx264 rejects a context allocated without one.
|
||||||
|
let mut context = unsafe { codec::context::Context::wrap(ff::sys::avcodec_alloc_context3(codec.as_ptr()), None) }.encoder().video().map_err(text)?;
|
||||||
|
context.set_width(width);
|
||||||
|
context.set_height(height);
|
||||||
|
// NVENC takes packed BGR0 directly, so the GPU path skips the CPU colour conversion to YUV420P.
|
||||||
|
let pixel = match config.encoder { crate::video::Encoder::Nvenc => Pixel::BGRZ, crate::video::Encoder::Vaapi => Pixel::NV12, _ => Pixel::YUV420P };
|
||||||
|
context.set_format(if config.encoder == crate::video::Encoder::Vaapi { Pixel::VAAPI } else { pixel });
|
||||||
|
if config.encoder == crate::video::Encoder::Vaapi { attach_vaapi(&mut context, width, height)?; }
|
||||||
|
context.set_time_base(Rational::new(1, config.fps as i32));
|
||||||
|
context.set_frame_rate(Some(Rational::new(config.fps as i32, 1)));
|
||||||
|
context.set_gop(config.gop);
|
||||||
|
context.set_max_b_frames(0);
|
||||||
|
context.set_bit_rate(config.target_bps as usize);
|
||||||
|
context.set_max_bit_rate(config.max_bps as usize);
|
||||||
|
// The CLI's -maxrate/-bufsize map to these raw fields, which the encoder wrappers read.
|
||||||
|
unsafe {
|
||||||
|
let raw = context.as_mut_ptr();
|
||||||
|
(*raw).rc_max_rate = config.max_bps;
|
||||||
|
(*raw).rc_buffer_size = config.buffer_bits as i32;
|
||||||
|
}
|
||||||
|
let cq = config.cq.to_string();
|
||||||
|
let gop = config.gop.to_string();
|
||||||
|
let mut options = Dictionary::new();
|
||||||
|
if config.encoder == crate::video::Encoder::Vaapi {
|
||||||
|
for (key,value) in [("profile", "constrained_baseline"), ("rc_mode", "VBR"), ("async_depth", "1"), ("aud", "1")] { options.set(key,value); }
|
||||||
|
options.set("qmin", &cq);
|
||||||
|
} else if config.encoder == crate::video::Encoder::Nvenc {
|
||||||
|
for (key, value) in [("preset", "p4"), ("tune", "ull"), ("profile", "baseline"), ("rc", "vbr"),
|
||||||
|
("rc-lookahead", "0"), ("zerolatency", "1"), ("delay", "0"), ("aud", "1"), ("forced-idr", "1")] {
|
||||||
|
options.set(key, value);
|
||||||
|
}
|
||||||
|
options.set("cq", &cq);
|
||||||
|
} else {
|
||||||
|
let x264 = format!("aud=1:repeat-headers=1:scenecut=0:keyint={gop}:min-keyint={gop}");
|
||||||
|
for (key, value) in [("preset", "superfast"), ("tune", "zerolatency"), ("profile", "baseline"), ("threads", "4")] {
|
||||||
|
options.set(key, value);
|
||||||
|
}
|
||||||
|
options.set("crf", &cq);
|
||||||
|
options.set("x264-params", &x264);
|
||||||
|
}
|
||||||
|
let encoder = context.open_as_with(codec, options).map_err(text)?;
|
||||||
|
Ok((encoder, pixel, width, height))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Each codec owns a reference to its hardware frame pool. Every error path releases
|
||||||
|
// the temporary device/pool refs; AVCodecContext releases its own ref on drop.
|
||||||
|
fn attach_vaapi(context: &mut encoder::video::Video, width: u32, height: u32) -> Result<(), String> {
|
||||||
|
use ff::sys::*;
|
||||||
|
let device_name = CString::new(crate::video::vaapi_device()).map_err(|_| "Invalid VAAPI device")?;
|
||||||
|
unsafe {
|
||||||
|
let mut device = std::ptr::null_mut();
|
||||||
|
let result = av_hwdevice_ctx_create(&mut device, AVHWDeviceType::AV_HWDEVICE_TYPE_VAAPI, device_name.as_ptr(), std::ptr::null_mut(), 0);
|
||||||
|
if result < 0 { return Err(format!("VAAPI device initialization failed: {}", text(ff::Error::from(result)))); }
|
||||||
|
let mut pool = av_hwframe_ctx_alloc(device);
|
||||||
|
av_buffer_unref(&mut device);
|
||||||
|
if pool.is_null() { return Err("VAAPI frame pool allocation failed".into()); }
|
||||||
|
let frames = (*pool).data as *mut AVHWFramesContext;
|
||||||
|
(*frames).format = AVPixelFormat::AV_PIX_FMT_VAAPI;
|
||||||
|
(*frames).sw_format = AVPixelFormat::AV_PIX_FMT_NV12;
|
||||||
|
(*frames).width = width as i32;
|
||||||
|
(*frames).height = height as i32;
|
||||||
|
(*frames).initial_pool_size = 8;
|
||||||
|
let result = av_hwframe_ctx_init(pool);
|
||||||
|
if result < 0 { av_buffer_unref(&mut pool); return Err(format!("VAAPI frame pool initialization failed: {}", text(ff::Error::from(result)))); }
|
||||||
|
(*context.as_mut_ptr()).hw_frames_ctx = pool;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn send_video_frame(encoder: &mut encoder::Video, frame: &frame::Video, kind: crate::video::Encoder) -> Result<(), String> {
|
||||||
|
if kind != crate::video::Encoder::Vaapi { return encoder.send_frame(frame).map_err(text); }
|
||||||
|
let mut uploaded = frame::Video::empty();
|
||||||
|
unsafe {
|
||||||
|
let result = ff::sys::av_hwframe_get_buffer((*encoder.as_ptr()).hw_frames_ctx, uploaded.as_mut_ptr(), 0);
|
||||||
|
if result < 0 { return Err(text(ff::Error::from(result))); }
|
||||||
|
let result = ff::sys::av_hwframe_transfer_data(uploaded.as_mut_ptr(), frame.as_ptr(), 0);
|
||||||
|
if result < 0 { return Err(text(ff::Error::from(result))); }
|
||||||
|
let result = ff::sys::av_frame_copy_props(uploaded.as_mut_ptr(), frame.as_ptr());
|
||||||
|
if result < 0 { return Err(text(ff::Error::from(result))); }
|
||||||
|
}
|
||||||
|
encoder.send_frame(&uploaded).map_err(text)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs until `stop` is set or the receiver is dropped. A set `idr` flag forces the next frame to be an IDR.
|
||||||
|
pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>) -> Result<(), String> {
|
||||||
|
ensure_init()?;
|
||||||
|
if config.damage {
|
||||||
|
match DamageCapture::open(&config.display) {
|
||||||
|
Ok(capture) => return run_damage(config, capture, units, stop, idr),
|
||||||
|
Err(error) => log::warn!("Damage-driven capture unavailable, using x11grab: {error}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let name = CString::new("x11grab").map_err(|e| e.to_string())?;
|
||||||
|
let raw = unsafe { ff::sys::av_find_input_format(name.as_ptr()) };
|
||||||
|
if raw.is_null() { return Err("x11grab input is not available in this FFmpeg build".into()); }
|
||||||
|
let input_format = Format::Input(unsafe { ff::format::format::Input::wrap(raw as *mut _) });
|
||||||
|
let mut capture_options = Dictionary::new();
|
||||||
|
capture_options.set("framerate", &config.fps.to_string());
|
||||||
|
capture_options.set("video_size", &format!("{}x{}", config.input.0, config.input.1));
|
||||||
|
// The viewer draws its own pointer; do not bake a second one into the video.
|
||||||
|
capture_options.set("draw_mouse", "0");
|
||||||
|
let mut input = ff::format::open_with(&config.display, &input_format, capture_options).map_err(text)?.input();
|
||||||
|
|
||||||
|
let stream = input.streams().best(ff::media::Type::Video).ok_or("X11 capture has no video stream")?;
|
||||||
|
let index = stream.index();
|
||||||
|
let mut decoder = codec::context::Context::from_parameters(stream.parameters()).map_err(text)?.decoder().video().map_err(text)?;
|
||||||
|
|
||||||
|
let (mut encoder, pixel, width, height) = open_encoder(&config)?;
|
||||||
|
|
||||||
|
let mut scaler: Option<(scaling::Context, (Pixel, u32, u32))> = None;
|
||||||
|
let mut decoded = frame::Video::empty();
|
||||||
|
let mut converted = frame::Video::new(pixel, width, height);
|
||||||
|
let mut packet = Packet::empty();
|
||||||
|
let started = Instant::now();
|
||||||
|
let frame_interval = Duration::from_nanos(1_000_000_000 / u64::from(config.fps));
|
||||||
|
let mut last_at: Option<Duration> = None;
|
||||||
|
let mut last_slot: i64 = -1;
|
||||||
|
for (stream, input_packet) in input.packets() {
|
||||||
|
if stop.load(Ordering::Relaxed) { return Ok(()); }
|
||||||
|
if stream.index() != index { continue; }
|
||||||
|
decoder.send_packet(&input_packet).map_err(text)?;
|
||||||
|
match decoder.receive_frame(&mut decoded) {
|
||||||
|
Ok(()) => {}
|
||||||
|
Err(ff::Error::Other { errno }) if errno == libc::EAGAIN => continue,
|
||||||
|
Err(error) => return Err(text(error)),
|
||||||
|
}
|
||||||
|
// x11grab catches up after a stall by grabbing back-to-back. A frame arriving within half
|
||||||
|
// a frame interval of the previous one is part of that burst and is dropped, as the old
|
||||||
|
// `-fps_mode vfr` did. Normal jitter stays well above that threshold.
|
||||||
|
let now = started.elapsed();
|
||||||
|
if last_at.is_some_and(|previous| now - previous < frame_interval / 2) { continue; }
|
||||||
|
last_at = Some(now);
|
||||||
|
let slot = ((now.as_micros() * u128::from(config.fps) + 500_000) / 1_000_000) as i64;
|
||||||
|
let slot = slot.max(last_slot + 1);
|
||||||
|
last_slot = slot;
|
||||||
|
|
||||||
|
// Pass the capture buffer straight through when it already matches the encoder input.
|
||||||
|
let frame = if decoded.format() == pixel && decoded.width() == width && decoded.height() == height {
|
||||||
|
&mut decoded
|
||||||
|
} else {
|
||||||
|
let key = (decoded.format(), decoded.width(), decoded.height());
|
||||||
|
if scaler.as_ref().map(|(_, k)| *k) != Some(key) {
|
||||||
|
let context = scaling::Context::get(key.0, key.1, key.2, pixel, width, height, scaling::Flags::BILINEAR).map_err(text)?;
|
||||||
|
scaler = Some((context, key));
|
||||||
|
}
|
||||||
|
if let Some((context, _)) = scaler.as_mut() { context.run(&decoded, &mut converted).map_err(text)?; }
|
||||||
|
&mut converted
|
||||||
|
};
|
||||||
|
|
||||||
|
frame.set_pts(Some(slot));
|
||||||
|
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
|
||||||
|
send_video_frame(&mut encoder, frame, config.encoder)?;
|
||||||
|
if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); }
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Damage-driven loop. Damage only marks the picture dirty; a dirty picture is captured and encoded on
|
||||||
|
/// the next fixed frame tick, so the stream keeps a steady cadence while the screen moves. The viewer's
|
||||||
|
/// jitter buffer grows with irregular arrivals, and irregular sends were what raised its latency.
|
||||||
|
/// An idle screen sends a keepalive about once a second so the viewer still sees traffic.
|
||||||
|
fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>) -> Result<(), String> {
|
||||||
|
let (mut encoder, pixel, width, height) = open_encoder(&config)?;
|
||||||
|
let (cap_width, cap_height) = capture.size();
|
||||||
|
let mut captured = frame::Video::new(Pixel::BGRZ, cap_width, cap_height);
|
||||||
|
let mut converted = frame::Video::new(pixel, width, height);
|
||||||
|
let mut scaler = None;
|
||||||
|
let mut packet = Packet::empty();
|
||||||
|
let started = Instant::now();
|
||||||
|
let frame_interval = Duration::from_nanos(1_000_000_000 / u64::from(config.fps));
|
||||||
|
// Keepalive gaps are random between 15 and 25 fps: a steady 1 fps reads as a stall to the viewer.
|
||||||
|
let mut seed = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(1, |d| d.subsec_nanos()) | 1;
|
||||||
|
let mut keepalive = random_keepalive(&mut seed);
|
||||||
|
let mut dirty = true;
|
||||||
|
let mut next_tick = Instant::now();
|
||||||
|
let mut last_send = Instant::now();
|
||||||
|
let mut last_slot: i64 = -1;
|
||||||
|
loop {
|
||||||
|
if stop.load(Ordering::Relaxed) { return Ok(()); }
|
||||||
|
let deadline = if dirty { next_tick } else { last_send + keepalive };
|
||||||
|
let wait = deadline.saturating_duration_since(Instant::now());
|
||||||
|
// A change marks the picture dirty and falls through, so a client that damages the root constantly
|
||||||
|
// cannot keep an overdue tick waiting.
|
||||||
|
if capture.wait(wait.max(Duration::from_millis(1)))? { dirty = true; }
|
||||||
|
let now = Instant::now();
|
||||||
|
if dirty && now < next_tick { continue; }
|
||||||
|
if !dirty && now < last_send + keepalive { continue; }
|
||||||
|
|
||||||
|
let source = capture.capture()?;
|
||||||
|
let bytes_per_line = cap_width as usize * 4;
|
||||||
|
let stride = captured.stride(0);
|
||||||
|
let row = bytes_per_line.min(stride);
|
||||||
|
for (y, src) in source.chunks_exact(bytes_per_line).enumerate() {
|
||||||
|
captured.data_mut(0)[y * stride..y * stride + row].copy_from_slice(&src[..row]);
|
||||||
|
}
|
||||||
|
|
||||||
|
let slot = ((started.elapsed().as_micros() * u128::from(config.fps) + 500_000) / 1_000_000) as i64;
|
||||||
|
let slot = slot.max(last_slot + 1);
|
||||||
|
last_slot = slot;
|
||||||
|
|
||||||
|
let frame = if pixel == Pixel::BGRZ && width == cap_width && height == cap_height {
|
||||||
|
&mut captured
|
||||||
|
} else {
|
||||||
|
if scaler.is_none() {
|
||||||
|
scaler = Some(scaling::Context::get(Pixel::BGRZ, cap_width, cap_height, pixel, width, height, scaling::Flags::BILINEAR).map_err(text)?);
|
||||||
|
}
|
||||||
|
if let Some(context) = scaler.as_mut() { context.run(&captured, &mut converted).map_err(text)?; }
|
||||||
|
&mut converted
|
||||||
|
};
|
||||||
|
frame.set_pts(Some(slot));
|
||||||
|
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
|
||||||
|
send_video_frame(&mut encoder, frame, config.encoder)?;
|
||||||
|
if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); }
|
||||||
|
let now = Instant::now();
|
||||||
|
if dirty {
|
||||||
|
// Keep the tick phase; after a long idle, restart the phase from now instead of bursting to catch up.
|
||||||
|
let next = next_tick + frame_interval;
|
||||||
|
next_tick = if next > now { next } else { now + frame_interval };
|
||||||
|
dirty = false;
|
||||||
|
}
|
||||||
|
last_send = now;
|
||||||
|
keepalive = random_keepalive(&mut seed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Picks the next keepalive gap in 40–67 ms (15–25 fps) with a small xorshift generator.
|
||||||
|
fn random_keepalive(seed: &mut u32) -> Duration {
|
||||||
|
*seed ^= *seed << 13;
|
||||||
|
*seed ^= *seed >> 17;
|
||||||
|
*seed ^= *seed << 5;
|
||||||
|
Duration::from_micros(40_000 + u64::from(*seed % 26_700))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Encodes a PulseAudio/PipeWire source to Opus in this process. Each Opus packet is sent as is.
|
||||||
|
pub fn run_audio(source: &str, units: mpsc::Sender<bytes::Bytes>, stop: Arc<AtomicBool>) -> Result<(), String> {
|
||||||
|
ensure_init()?;
|
||||||
|
let name = CString::new("pulse").map_err(|e| e.to_string())?;
|
||||||
|
let raw = unsafe { ff::sys::av_find_input_format(name.as_ptr()) };
|
||||||
|
if raw.is_null() { return Err("pulse input is not available in this FFmpeg build".into()); }
|
||||||
|
let input_format = Format::Input(unsafe { ff::format::format::Input::wrap(raw as *mut _) });
|
||||||
|
let mut capture_options = Dictionary::new();
|
||||||
|
capture_options.set("fragment_size", "1920");
|
||||||
|
let mut input = ff::format::open_with(&source, &input_format, capture_options).map_err(text)?.input();
|
||||||
|
|
||||||
|
let stream = input.streams().best(ff::media::Type::Audio).ok_or("Pulse source has no audio stream")?;
|
||||||
|
let index = stream.index();
|
||||||
|
let mut decoder = codec::context::Context::from_parameters(stream.parameters()).map_err(text)?.decoder().audio().map_err(text)?;
|
||||||
|
|
||||||
|
let codec = encoder::find_by_name("libopus").ok_or("Opus encoder is not available in this FFmpeg build")?;
|
||||||
|
let mut context = unsafe { codec::context::Context::wrap(ff::sys::avcodec_alloc_context3(codec.as_ptr()), None) }.encoder().audio().map_err(text)?;
|
||||||
|
context.set_rate(48_000);
|
||||||
|
context.set_format(Sample::I16(sample::Type::Packed));
|
||||||
|
// FFmpeg 6 reads the AVChannelLayout field; the deprecated channel_layout alone makes libopus fail with EINVAL.
|
||||||
|
unsafe { ff::sys::av_channel_layout_default(&mut (*context.as_mut_ptr()).ch_layout, 2) };
|
||||||
|
context.set_time_base(Rational::new(1, 48_000));
|
||||||
|
context.set_bit_rate(128_000);
|
||||||
|
let mut options = Dictionary::new();
|
||||||
|
options.set("application", "lowdelay");
|
||||||
|
options.set("frame_duration", "20");
|
||||||
|
let mut encoder = context.open_as_with(codec, options).map_err(text)?;
|
||||||
|
// libopus takes fixed-size frames: 960 samples per channel for 20 ms.
|
||||||
|
let frame_samples = match encoder.frame_size() as usize { 0 => 960, n => n };
|
||||||
|
|
||||||
|
let mut resampler = None;
|
||||||
|
let mut decoded = frame::Audio::empty();
|
||||||
|
let mut converted = frame::Audio::empty();
|
||||||
|
let mut fifo: Vec<i16> = Vec::new();
|
||||||
|
let mut pts: i64 = 0;
|
||||||
|
let mut packet = Packet::empty();
|
||||||
|
for (stream, input_packet) in input.packets() {
|
||||||
|
if stop.load(Ordering::Relaxed) { return Ok(()); }
|
||||||
|
if stream.index() != index { continue; }
|
||||||
|
decoder.send_packet(&input_packet).map_err(text)?;
|
||||||
|
loop {
|
||||||
|
match decoder.receive_frame(&mut decoded) {
|
||||||
|
Ok(()) => {}
|
||||||
|
Err(ff::Error::Other { errno }) if errno == libc::EAGAIN => break,
|
||||||
|
Err(error) => return Err(text(error)),
|
||||||
|
}
|
||||||
|
// The decoder may leave the deprecated channel_layout unset; derive it from the channel count.
|
||||||
|
let source_layout = ChannelLayout::default(i32::from(decoded.channels()));
|
||||||
|
if resampler.is_none() {
|
||||||
|
resampler = Some(ff::software::resampling::Context::get(
|
||||||
|
decoded.format(), source_layout, decoded.rate(),
|
||||||
|
Sample::I16(sample::Type::Packed), ChannelLayout::STEREO, 48_000,
|
||||||
|
).map_err(text)?);
|
||||||
|
}
|
||||||
|
// Keep both layout fields of the decoded frame in step with the resampler's input layout.
|
||||||
|
decoded.set_channel_layout(source_layout);
|
||||||
|
unsafe { ff::sys::av_channel_layout_default(&mut (*decoded.as_mut_ptr()).ch_layout, i32::from(decoded.channels())) };
|
||||||
|
if let Some(resampler) = resampler.as_mut() { resampler.run(&decoded, &mut converted).map_err(text)?; }
|
||||||
|
let samples = converted.samples() * 2;
|
||||||
|
fifo.extend(converted.data(0)[..samples * 2].chunks_exact(2).map(|b| i16::from_ne_bytes([b[0], b[1]])));
|
||||||
|
|
||||||
|
while fifo.len() >= frame_samples * 2 {
|
||||||
|
let mut out = frame::Audio::new(Sample::I16(sample::Type::Packed), frame_samples, ChannelLayout::STEREO);
|
||||||
|
unsafe { ff::sys::av_channel_layout_default(&mut (*out.as_mut_ptr()).ch_layout, 2) };
|
||||||
|
out.set_rate(48_000);
|
||||||
|
out.set_pts(Some(pts));
|
||||||
|
for (dst, src) in out.data_mut(0)[..frame_samples * 4].chunks_exact_mut(2).zip(fifo.drain(..frame_samples * 2)) {
|
||||||
|
dst.copy_from_slice(&src.to_ne_bytes());
|
||||||
|
}
|
||||||
|
pts += frame_samples as i64;
|
||||||
|
encoder.send_frame(&out).map_err(text)?;
|
||||||
|
loop {
|
||||||
|
match encoder.receive_packet(&mut packet) {
|
||||||
|
Ok(()) => {
|
||||||
|
let data = bytes::Bytes::copy_from_slice(packet.data().unwrap_or_default());
|
||||||
|
if units.blocking_send(data).is_err() { return Ok(()); }
|
||||||
|
}
|
||||||
|
Err(ff::Error::Other { errno }) if errno == libc::EAGAIN => break,
|
||||||
|
Err(error) => return Err(text(error)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
+311
-5
@@ -8,7 +8,11 @@ extern "C" {
|
|||||||
fn x11_pointer_state(x:*mut libc::c_int,y:*mut libc::c_int,mask:*mut libc::c_uint)->libc::c_int;
|
fn x11_pointer_state(x:*mut libc::c_int,y:*mut libc::c_int,mask:*mut libc::c_uint)->libc::c_int;
|
||||||
fn x11_key_symbol(symbol: libc::c_ulong, is_press: libc::c_int) -> libc::c_int;
|
fn x11_key_symbol(symbol: libc::c_ulong, is_press: libc::c_int) -> libc::c_int;
|
||||||
fn x11_key_event(keycode: libc::c_int, is_press: libc::c_int) -> libc::c_int;
|
fn x11_key_event(keycode: libc::c_int, is_press: libc::c_int) -> libc::c_int;
|
||||||
|
fn x11_release_all() -> libc::c_int;
|
||||||
|
fn x11_terminal_shortcut_state() -> libc::c_int;
|
||||||
}
|
}
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
|
unsafe fn x11_terminal_shortcut_state() -> libc::c_int { 0 }
|
||||||
|
|
||||||
#[cfg(not(target_os = "linux"))]
|
#[cfg(not(target_os = "linux"))]
|
||||||
unsafe fn x11_mouse_move(_: libc::c_int, _: libc::c_int) -> libc::c_int { 0 }
|
unsafe fn x11_mouse_move(_: libc::c_int, _: libc::c_int) -> libc::c_int { 0 }
|
||||||
@@ -23,6 +27,9 @@ unsafe fn x11_key_symbol(_: libc::c_ulong, _: libc::c_int) -> libc::c_int { 0 }
|
|||||||
#[cfg(not(target_os = "linux"))]
|
#[cfg(not(target_os = "linux"))]
|
||||||
unsafe fn x11_mouse_relative(_: libc::c_int, _: libc::c_int) -> libc::c_int {0}
|
unsafe fn x11_mouse_relative(_: libc::c_int, _: libc::c_int) -> libc::c_int {0}
|
||||||
|
|
||||||
|
#[cfg(not(target_os="linux"))]
|
||||||
|
unsafe fn x11_release_all()->i32 {0}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct PendingPointer {
|
struct PendingPointer {
|
||||||
absolute: Option<(i32,i32)>,
|
absolute: Option<(i32,i32)>,
|
||||||
@@ -33,11 +40,26 @@ struct PendingPointer {
|
|||||||
unsafe fn x11_pointer_state(_: *mut libc::c_int,_:*mut libc::c_int,_:*mut libc::c_uint)->libc::c_int {-1}
|
unsafe fn x11_pointer_state(_: *mut libc::c_int,_:*mut libc::c_int,_:*mut libc::c_uint)->libc::c_int {-1}
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct HeldInput { buttons: std::collections::BTreeSet<i32>, keys: std::collections::BTreeSet<u64> }
|
struct HeldInput { buttons: std::collections::BTreeSet<i32>, keys: std::collections::BTreeSet<u64> }
|
||||||
pub struct InputInjector { portal_failures: std::sync::atomic::AtomicUsize, portal_outcomes: [std::sync::atomic::AtomicUsize; 3], portal:std::sync::Mutex<Option<std::sync::Arc<crate::portal::PortalSession>>>, backend: crate::desktop_backend::DesktopBackend, pending: std::sync::Mutex<PendingPointer>, held: std::sync::Mutex<HeldInput> }
|
/// Held modifier VKs (bit 0 Ctrl, 1 Shift, 2 Alt, 3 Win) and keys that got a synthetic Shift.
|
||||||
|
#[derive(Default)]
|
||||||
|
struct Shortcuts { modifiers: u8, shifted: std::collections::BTreeSet<i32> }
|
||||||
|
/// Mac viewer with 「使用 macOS 快捷键」 off sends Cmd as VK_LWIN (verified: Cmd 0x5B, Option 0xA4,
|
||||||
|
/// Ctrl 0xA2). Cmd is held back until we know whether it starts a Mac shortcut.
|
||||||
|
#[derive(Default)]
|
||||||
|
struct CmdState { held: bool, pending: bool, passthrough: bool, combos: std::collections::BTreeMap<i32, bool> }
|
||||||
|
|
||||||
|
pub struct InputInjector { mac_controller: std::sync::atomic::AtomicBool, cmd: std::sync::Mutex<CmdState>, serial:std::sync::Mutex<()>, owner:std::sync::Mutex<DesktopOwner>, shortcuts: std::sync::Mutex<Shortcuts>, uinput: Option<crate::uinput::Devices>, screen: std::sync::Mutex<(u32,u32)>, portal_failures: std::sync::atomic::AtomicUsize, portal_outcomes: [std::sync::atomic::AtomicUsize; 3], portal:std::sync::Mutex<Option<std::sync::Arc<crate::portal::PortalSession>>>, backend: crate::desktop_backend::DesktopBackend, pending: std::sync::Mutex<PendingPointer>, held: std::sync::Mutex<HeldInput> }
|
||||||
|
|
||||||
impl InputInjector {
|
impl InputInjector {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self { portal_failures: std::sync::atomic::AtomicUsize::new(0), portal_outcomes: std::array::from_fn(|_| std::sync::atomic::AtomicUsize::new(0)), portal:std::sync::Mutex::new(None), backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11), pending: std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()) }
|
let backend = crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11);
|
||||||
|
let uinput = if crate::uinput::wanted(backend) {
|
||||||
|
match crate::uinput::Devices::open() {
|
||||||
|
Ok(devices) => { log::info!("Input backend: uinput (kernel virtual keyboard/pointer)"); Some(devices) }
|
||||||
|
Err(e) => { log::warn!("uinput unavailable ({}); using the desktop input path. See README for the udev rule.", e.kind()); None }
|
||||||
|
}
|
||||||
|
} else { None };
|
||||||
|
Self { mac_controller: std::sync::atomic::AtomicBool::new(false), cmd: std::sync::Mutex::new(CmdState::default()), serial:std::sync::Mutex::new(()), owner:std::sync::Mutex::new(DesktopOwner::default()), shortcuts: std::sync::Mutex::new(Shortcuts::default()), uinput, screen: std::sync::Mutex::new((0,0)), portal_failures: std::sync::atomic::AtomicUsize::new(0), portal_outcomes: std::array::from_fn(|_| std::sync::atomic::AtomicUsize::new(0)), portal:std::sync::Mutex::new(None), backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11), pending: std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bind_portal(&self,session:Option<std::sync::Arc<crate::portal::PortalSession>>) {
|
pub fn bind_portal(&self,session:Option<std::sync::Arc<crate::portal::PortalSession>>) {
|
||||||
@@ -45,10 +67,12 @@ impl InputInjector {
|
|||||||
*self.portal.lock().unwrap()=session;
|
*self.portal.lock().unwrap()=session;
|
||||||
}
|
}
|
||||||
pub fn mouse_move(&self, x: i32, y: i32) {
|
pub fn mouse_move(&self, x: i32, y: i32) {
|
||||||
|
let _serial=self.serial.lock().unwrap();
|
||||||
self.inject_move(x,y,false);
|
self.inject_move(x,y,false);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mouse_absolute_normalized(&self, x: f64, y: f64, size: (u32,u32)) {
|
pub fn mouse_absolute_normalized(&self, x: f64, y: f64, size: (u32,u32)) {
|
||||||
|
*self.screen.lock().unwrap() = size;
|
||||||
if let Some(position) = crate::input_mapping::normalized_pointer(x,y,size.0,size.1) {
|
if let Some(position) = crate::input_mapping::normalized_pointer(x,y,size.0,size.1) {
|
||||||
let mut pending = self.pending.lock().unwrap();
|
let mut pending = self.pending.lock().unwrap();
|
||||||
pending.absolute = Some(position);
|
pending.absolute = Some(position);
|
||||||
@@ -78,6 +102,10 @@ impl InputInjector {
|
|||||||
}
|
}
|
||||||
pub fn discard_pointer(&self) { *self.pending.lock().unwrap() = PendingPointer::default(); }
|
pub fn discard_pointer(&self) { *self.pending.lock().unwrap() = PendingPointer::default(); }
|
||||||
pub fn flush_pointer(&self) {
|
pub fn flush_pointer(&self) {
|
||||||
|
let _serial=self.serial.lock().unwrap();
|
||||||
|
self.flush_pointer_locked();
|
||||||
|
}
|
||||||
|
fn flush_pointer_locked(&self) {
|
||||||
// Hold the queue lock through injection so an older timer tick cannot
|
// Hold the queue lock through injection so an older timer tick cannot
|
||||||
// overwrite a newer position flushed before a button transition.
|
// overwrite a newer position flushed before a button transition.
|
||||||
let mut pending = self.pending.lock().unwrap();
|
let mut pending = self.pending.lock().unwrap();
|
||||||
@@ -103,7 +131,8 @@ impl InputInjector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn mouse_button(&self, button: i32, is_press: bool) {
|
pub fn mouse_button(&self, button: i32, is_press: bool) {
|
||||||
self.flush_pointer();
|
let _serial=self.serial.lock().unwrap();
|
||||||
|
self.flush_pointer_locked();
|
||||||
let result = self.inject_button(button,is_press);
|
let result = self.inject_button(button,is_press);
|
||||||
if result == 0 {
|
if result == 0 {
|
||||||
let mut held = self.held.lock().unwrap();
|
let mut held = self.held.lock().unwrap();
|
||||||
@@ -117,7 +146,102 @@ impl InputInjector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Mac viewers send Cmd+V as Ctrl+V (verified: Cmd and Ctrl both arrive as VK_LCONTROL), which in
|
||||||
|
/// a Linux terminal quotes the next key instead of pasting. In a focused terminal Ctrl+V becomes
|
||||||
|
/// Ctrl+Shift+V. Ctrl+C is never translated: it must always interrupt, and copy is Cmd+Shift+C
|
||||||
|
/// (= Ctrl+Shift+C). X11 only; `UU_TERMINAL_SHORTCUTS=0` disables.
|
||||||
|
fn terminal_shift(&self, key: i32, is_press: bool) -> bool {
|
||||||
|
let bit = match key { 0x11 | 0xa2 | 0xa3 => 1, 0x10 | 0xa0 | 0xa1 => 2, 0x12 | 0xa4 | 0xa5 => 4, 0x5b | 0x5c => 8, _ => 0 };
|
||||||
|
let mut shortcuts = self.shortcuts.lock().unwrap();
|
||||||
|
if bit != 0 {
|
||||||
|
if is_press { shortcuts.modifiers |= bit } else { shortcuts.modifiers &= !bit }
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if !is_press { return shortcuts.shifted.remove(&key); }
|
||||||
|
if key != 0x56 || shortcuts.modifiers != 1 { return false; }
|
||||||
|
if std::env::var("UU_TERMINAL_SHORTCUTS").is_ok_and(|v| v == "0") || std::env::var_os("DISPLAY").is_none() { return false; }
|
||||||
|
let shift = unsafe { x11_terminal_shortcut_state() } & 1 != 0;
|
||||||
|
if shift {
|
||||||
|
shortcuts.shifted.insert(key);
|
||||||
|
static NOTED: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||||
|
if NOTED.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 4 {
|
||||||
|
log::info!("Terminal shortcut: Ctrl+V sent as Ctrl+Shift+V");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
shift
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The current desktop controller is the macOS viewer (connect header controller_platform).
|
||||||
|
pub fn set_mac_controller(&self, mac: bool) {
|
||||||
|
self.mac_controller.store(mac, std::sync::atomic::Ordering::Relaxed);
|
||||||
|
*self.cmd.lock().unwrap() = CmdState::default();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn terminal_focused(&self) -> bool {
|
||||||
|
self.backend == crate::desktop_backend::DesktopBackend::X11 && std::env::var_os("DISPLAY").is_some()
|
||||||
|
&& !std::env::var("UU_TERMINAL_SHORTCUTS").is_ok_and(|v| v == "0")
|
||||||
|
&& unsafe { x11_terminal_shortcut_state() } & 1 != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Mac habits for a Mac viewer with 「使用 macOS 快捷键」 off: Cmd+C/V/A/F/X/Z (the keys that option
|
||||||
|
/// lists) become Ctrl+key, and in a focused terminal Cmd+C/V become Ctrl+Shift+C/V (copy/paste)
|
||||||
|
/// while Ctrl+C still interrupts. Other Cmd combos, and Cmd alone, stay Super. Returns true when handled.
|
||||||
|
fn mac_cmd(&self, key: i32, is_press: bool) -> bool {
|
||||||
|
const SUPER_L: u64 = 0xffeb; const CONTROL_L: u64 = 0xffe3; const SHIFT_L: u64 = 0xffe1;
|
||||||
|
let mut st = self.cmd.lock().unwrap();
|
||||||
|
if matches!(key, 0x5b | 0x5c) {
|
||||||
|
if is_press { st.held = true; st.pending = true; st.passthrough = false; }
|
||||||
|
else {
|
||||||
|
if st.passthrough { self.inject_key(SUPER_L, false); }
|
||||||
|
else if st.pending { self.inject_key(SUPER_L, true); self.inject_key(SUPER_L, false); }
|
||||||
|
st.held = false; st.pending = false; st.passthrough = false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if !is_press {
|
||||||
|
let Some(shifted) = st.combos.remove(&key) else { return false };
|
||||||
|
self.key_symbol_event(key, false);
|
||||||
|
if shifted { self.inject_key(SHIFT_L, false); }
|
||||||
|
self.inject_key(CONTROL_L, false);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if !st.held { return false; }
|
||||||
|
// Shift/Option/Ctrl pressed with Cmd (e.g. Cmd+Shift+Z) must not decide the combo yet.
|
||||||
|
if matches!(key, 0x10..=0x12 | 0xa0..=0xa5) { return false; }
|
||||||
|
if st.combos.contains_key(&key) { self.key_symbol_event(key, true); return true; } // autorepeat
|
||||||
|
if matches!(key, 0x41 | 0x43 | 0x46 | 0x56 | 0x58 | 0x5a) && !st.passthrough {
|
||||||
|
st.pending = false;
|
||||||
|
let shifted = matches!(key, 0x43 | 0x56) && self.terminal_focused();
|
||||||
|
self.inject_key(CONTROL_L, true);
|
||||||
|
if shifted { self.inject_key(SHIFT_L, true); }
|
||||||
|
self.key_symbol_event(key, true);
|
||||||
|
st.combos.insert(key, shifted);
|
||||||
|
static NOTED: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||||
|
if NOTED.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 4 { log::info!("Mac shortcut: Cmd combo sent as Ctrl{}", if shifted { "+Shift (terminal)" } else { "" }); }
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if st.pending { self.inject_key(SUPER_L, true); st.pending = false; st.passthrough = true; }
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
pub fn windows_key_event(&self, key: i32, is_press: bool) {
|
pub fn windows_key_event(&self, key: i32, is_press: bool) {
|
||||||
|
let _serial=self.serial.lock().unwrap();
|
||||||
|
if self.mac_controller.load(std::sync::atomic::Ordering::Relaxed) && self.mac_cmd(key, is_press) { return; }
|
||||||
|
if self.terminal_shift(key, is_press) {
|
||||||
|
const SHIFT_L: u64 = 0xffe1;
|
||||||
|
if is_press {
|
||||||
|
self.inject_key(SHIFT_L, true);
|
||||||
|
self.key_symbol_event(key, true);
|
||||||
|
} else {
|
||||||
|
self.key_symbol_event(key, false);
|
||||||
|
self.inject_key(SHIFT_L, false);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.key_symbol_event(key, is_press);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn key_symbol_event(&self, key: i32, is_press: bool) {
|
||||||
if let Some(symbol) = crate::input_mapping::windows_vk_to_keysym(key) {
|
if let Some(symbol) = crate::input_mapping::windows_vk_to_keysym(key) {
|
||||||
let result = self.inject_key(symbol,is_press);
|
let result = self.inject_key(symbol,is_press);
|
||||||
static AUDIT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
static AUDIT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||||
@@ -132,11 +256,30 @@ impl InputInjector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn release_held(&self) {
|
pub fn release_held(&self) {
|
||||||
|
let _serial=self.serial.lock().unwrap();
|
||||||
self.discard_pointer();
|
self.discard_pointer();
|
||||||
let mut held=self.held.lock().unwrap();
|
let mut held=self.held.lock().unwrap();
|
||||||
for button in &held.buttons {self.inject_button(*button,false);}
|
if let Some(uinput)=&self.uinput {
|
||||||
for key in &held.keys {self.inject_key(*key,false);}
|
Self::uinput_result(uinput.release_all());
|
||||||
|
} else if self.backend==crate::desktop_backend::DesktopBackend::X11 {
|
||||||
|
unsafe {x11_release_all();}
|
||||||
|
} else if self.backend==crate::desktop_backend::DesktopBackend::Wayland && self.portal.lock().unwrap().is_none() {
|
||||||
|
crate::wayland::release_all();
|
||||||
|
} else {
|
||||||
|
// Portal/Wayland cannot query global state. Sweep every key/button
|
||||||
|
// that this host can emit through the currently bound session.
|
||||||
|
let keys:std::collections::BTreeSet<_>=(0..=255).filter_map(crate::input_mapping::windows_vk_to_keysym)
|
||||||
|
.chain(held.keys.iter().copied()).collect();
|
||||||
|
// A dead Portal bus can time out per call. Stop on the first
|
||||||
|
// failure rather than blocking teardown for hundreds of calls;
|
||||||
|
// the next connected session retries the complete sweep.
|
||||||
|
let buttons_ok=[1,2,3,8,9].into_iter().all(|button|self.inject_button(button,false)==0);
|
||||||
|
let keys_ok=buttons_ok && keys.into_iter().all(|key|self.inject_key(key,false)==0);
|
||||||
|
if !keys_ok {log::warn!("Desktop input reset incomplete: backend unavailable");}
|
||||||
|
}
|
||||||
*held=HeldInput::default();
|
*held=HeldInput::default();
|
||||||
|
*self.shortcuts.lock().unwrap()=Shortcuts::default();
|
||||||
|
*self.cmd.lock().unwrap()=CmdState::default();
|
||||||
}
|
}
|
||||||
// Per-injector budgets cover missing bindings, locked sessions, stale handles
|
// Per-injector budgets cover missing bindings, locked sessions, stale handles
|
||||||
// and D-Bus errors. Never include keys, coordinates, text or portal tokens.
|
// and D-Bus errors. Never include keys, coordinates, text or portal tokens.
|
||||||
@@ -158,25 +301,54 @@ impl InputInjector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fn uinput_result(result: std::io::Result<()>) -> i32 {
|
||||||
|
match result {
|
||||||
|
Ok(()) => 0,
|
||||||
|
Err(e) => {
|
||||||
|
static FAILURES: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||||
|
if FAILURES.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 8 { log::warn!("uinput injection failed: {}", e.kind()); }
|
||||||
|
-1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
fn inject_move(&self,x:i32,y:i32,relative:bool)->i32 {
|
fn inject_move(&self,x:i32,y:i32,relative:bool)->i32 {
|
||||||
|
if let Some(uinput)=&self.uinput {
|
||||||
|
if relative { return Self::uinput_result(uinput.move_relative(x,y)); }
|
||||||
|
let (w,h)=*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)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if self.backend==crate::desktop_backend::DesktopBackend::Kms { return -1; }
|
||||||
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("motion", portal.move_pointer(f64::from(x),f64::from(y),relative)); }
|
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("motion", portal.move_pointer(f64::from(x),f64::from(y),relative)); }
|
||||||
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("motion", Err("Portal input session is not bound".into()));}
|
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("motion", Err("Portal input session is not bound".into()));}
|
||||||
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::move_pointer(x,y,relative) }
|
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::move_pointer(x,y,relative) }
|
||||||
else if relative { unsafe { x11_mouse_relative(x,y) } } else { unsafe { x11_mouse_move(x,y) } }
|
else if relative { unsafe { x11_mouse_relative(x,y) } } else { unsafe { x11_mouse_move(x,y) } }
|
||||||
}
|
}
|
||||||
fn inject_button(&self,button:i32,pressed:bool)->i32 {
|
fn inject_button(&self,button:i32,pressed:bool)->i32 {
|
||||||
|
if let Some(uinput)=&self.uinput {
|
||||||
|
if let Some((v,h))=crate::uinput::wheel_step(button) { return if pressed { Self::uinput_result(uinput.wheel(v,h)) } else { 0 }; }
|
||||||
|
if let Some(code)=crate::uinput::button_code(button) { return Self::uinput_result(uinput.button(code,pressed)); }
|
||||||
|
}
|
||||||
|
if self.backend==crate::desktop_backend::DesktopBackend::Kms { return -1; }
|
||||||
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("button", portal.button(button,pressed)); }
|
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("button", portal.button(button,pressed)); }
|
||||||
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("button", Err("Portal input session is not bound".into()));}
|
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("button", Err("Portal input session is not bound".into()));}
|
||||||
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::button(button,pressed) }
|
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::button(button,pressed) }
|
||||||
else { unsafe { x11_mouse_button(button,i32::from(pressed)) } }
|
else { unsafe { x11_mouse_button(button,i32::from(pressed)) } }
|
||||||
}
|
}
|
||||||
fn inject_key(&self,symbol:u64,pressed:bool)->i32 {
|
fn inject_key(&self,symbol:u64,pressed:bool)->i32 {
|
||||||
|
if let Some(uinput)=&self.uinput {
|
||||||
|
if let Some(code)=crate::uinput::keysym_to_evdev(symbol) { return Self::uinput_result(uinput.key(code,pressed)); }
|
||||||
|
}
|
||||||
|
if self.backend==crate::desktop_backend::DesktopBackend::Kms { return -1; }
|
||||||
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("keyboard", portal.key(symbol,pressed)); }
|
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("keyboard", portal.key(symbol,pressed)); }
|
||||||
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("keyboard", Err("Portal input session is not bound".into()));}
|
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("keyboard", Err("Portal input session is not bound".into()));}
|
||||||
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::key(symbol,pressed) }
|
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::key(symbol,pressed) }
|
||||||
else { unsafe { x11_key_symbol(symbol as libc::c_ulong,i32::from(pressed)) } }
|
else { unsafe { x11_key_symbol(symbol as libc::c_ulong,i32::from(pressed)) } }
|
||||||
}
|
}
|
||||||
pub fn key_event(&self, keycode: i32, is_press: bool) {
|
pub fn key_event(&self, keycode: i32, is_press: bool) {
|
||||||
|
let _serial=self.serial.lock().unwrap();
|
||||||
if self.backend!=crate::desktop_backend::DesktopBackend::X11 { return; }
|
if self.backend!=crate::desktop_backend::DesktopBackend::X11 { return; }
|
||||||
unsafe { x11_key_event(keycode, if is_press { 1 } else { 0 }) };
|
unsafe { x11_key_event(keycode, if is_press { 1 } else { 0 }) };
|
||||||
}
|
}
|
||||||
@@ -213,6 +385,29 @@ mod motion_tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod shortcut_tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn modifiers_are_tracked_and_non_terminal_keys_pass_through() {
|
||||||
|
let input = InputInjector::new();
|
||||||
|
assert!(!input.terminal_shift(0x11, true));
|
||||||
|
assert_eq!(input.shortcuts.lock().unwrap().modifiers, 1);
|
||||||
|
// No X display in tests: Ctrl+V is not translated.
|
||||||
|
assert!(!input.terminal_shift(0x56, true));
|
||||||
|
assert!(!input.terminal_shift(0x56, false));
|
||||||
|
assert!(!input.terminal_shift(0x11, false));
|
||||||
|
assert_eq!(input.shortcuts.lock().unwrap().modifiers, 0);
|
||||||
|
// A translated key keeps its synthetic Shift until its own release.
|
||||||
|
input.shortcuts.lock().unwrap().shifted.insert(0x56);
|
||||||
|
assert!(input.terminal_shift(0x56, false));
|
||||||
|
assert!(!input.terminal_shift(0x56, false));
|
||||||
|
// Ctrl+C is never translated.
|
||||||
|
input.terminal_shift(0x11, true);
|
||||||
|
assert!(!input.terminal_shift(0x43, true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod scroll_tests {
|
mod scroll_tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -227,3 +422,114 @@ mod scroll_tests {
|
|||||||
assert_eq!(input.pending.lock().unwrap().scroll,(-5,-5));
|
assert_eq!(input.pending.lock().unwrap().scroll,(-5,-5));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Only the newest desktop lease may inject or release input. Tool peers never
|
||||||
|
/// obtain a lease; delayed callbacks from an old desktop cannot clear a new one.
|
||||||
|
#[derive(Default)]
|
||||||
|
struct DesktopOwner { next:u64, current:u64, active:bool, stopped:bool }
|
||||||
|
impl DesktopOwner {
|
||||||
|
fn replace(&mut self)->u64 {self.next+=1;self.current=self.next;self.active=false;self.current}
|
||||||
|
fn owns(&self,id:u64)->bool {id!=0 && self.current==id}
|
||||||
|
}
|
||||||
|
pub struct DesktopInputSession {input:std::sync::Arc<InputInjector>,id:u64}
|
||||||
|
impl InputInjector {
|
||||||
|
pub fn shutdown(&self) {
|
||||||
|
let mut owner=self.owner.lock().unwrap();
|
||||||
|
owner.current=0;owner.active=false;owner.stopped=true;
|
||||||
|
self.release_held();
|
||||||
|
}
|
||||||
|
pub fn desktop_session(self:&std::sync::Arc<Self>)->std::sync::Arc<DesktopInputSession> {
|
||||||
|
let mut owner=self.owner.lock().unwrap();
|
||||||
|
let id=if owner.stopped {0} else {self.release_held();owner.replace()};
|
||||||
|
std::sync::Arc::new(DesktopInputSession {input:self.clone(),id})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl DesktopInputSession {
|
||||||
|
pub fn connected(&self) {
|
||||||
|
let mut owner=self.input.owner.lock().unwrap();
|
||||||
|
if owner.owns(self.id) && !owner.active {self.input.release_held();owner.active=true;}
|
||||||
|
}
|
||||||
|
pub fn disconnected(&self) {
|
||||||
|
let mut owner=self.input.owner.lock().unwrap();
|
||||||
|
if owner.owns(self.id) && owner.active {owner.active=false;self.input.release_held();}
|
||||||
|
}
|
||||||
|
pub fn with_input(&self, action:impl FnOnce()) {
|
||||||
|
let owner=self.input.owner.lock().unwrap();
|
||||||
|
if owner.owns(self.id) && owner.active {action();}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Drop for DesktopInputSession {fn drop(&mut self) {self.disconnected();}}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod lifecycle_tests {
|
||||||
|
use super::*;
|
||||||
|
use std::sync::{Arc,atomic::{AtomicUsize,Ordering}};
|
||||||
|
#[test]
|
||||||
|
fn cleanup_discards_motion_shortcuts_and_untracked_input_ledger() {
|
||||||
|
let input=Arc::new(InputInjector::new());
|
||||||
|
input.held.lock().unwrap().keys.insert(0xffe1);
|
||||||
|
input.held.lock().unwrap().buttons.insert(1);
|
||||||
|
input.shortcuts.lock().unwrap().modifiers=2;
|
||||||
|
input.shortcuts.lock().unwrap().shifted.insert(0x56);
|
||||||
|
input.mouse_relative(32,0);
|
||||||
|
input.mouse_scroll(0,1);
|
||||||
|
input.release_held();
|
||||||
|
assert!(input.held.lock().unwrap().keys.is_empty());
|
||||||
|
assert!(input.held.lock().unwrap().buttons.is_empty());
|
||||||
|
assert_eq!(input.pending.lock().unwrap().relative,(0,0));
|
||||||
|
assert_eq!(input.pending.lock().unwrap().scroll,(0,0));
|
||||||
|
assert_eq!(input.shortcuts.lock().unwrap().modifiers,0);
|
||||||
|
assert!(input.shortcuts.lock().unwrap().shifted.is_empty());
|
||||||
|
input.release_held(); // idempotent even with an empty ledger
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn disconnected_or_replaced_desktop_cannot_inject_or_clear_new_input() {
|
||||||
|
let input=Arc::new(InputInjector::new());
|
||||||
|
let count=AtomicUsize::new(0);
|
||||||
|
let old=input.desktop_session();
|
||||||
|
old.with_input(||{count.fetch_add(1,Ordering::Relaxed);});
|
||||||
|
assert_eq!(count.load(Ordering::Relaxed),0);
|
||||||
|
old.connected();
|
||||||
|
old.with_input(||{count.fetch_add(1,Ordering::Relaxed);});
|
||||||
|
old.disconnected();
|
||||||
|
old.with_input(||{count.fetch_add(1,Ordering::Relaxed);});
|
||||||
|
assert_eq!(count.load(Ordering::Relaxed),1);
|
||||||
|
input.held.lock().unwrap().buttons.insert(2);
|
||||||
|
old.disconnected(); // repeated close states must not release later local activity
|
||||||
|
assert!(input.held.lock().unwrap().buttons.contains(&2));
|
||||||
|
old.connected(); // same peer can reconnect
|
||||||
|
assert!(input.held.lock().unwrap().buttons.is_empty());
|
||||||
|
let new=input.desktop_session();
|
||||||
|
new.connected();
|
||||||
|
input.held.lock().unwrap().buttons.insert(1);
|
||||||
|
old.disconnected();old.connected();drop(old);
|
||||||
|
assert!(input.held.lock().unwrap().buttons.contains(&1));
|
||||||
|
new.with_input(||{count.fetch_add(1,Ordering::Relaxed);});
|
||||||
|
assert_eq!(count.load(Ordering::Relaxed),2);
|
||||||
|
drop(new);
|
||||||
|
assert!(input.held.lock().unwrap().buttons.is_empty());
|
||||||
|
let last=input.desktop_session();last.connected();
|
||||||
|
input.shutdown();last.connected();
|
||||||
|
last.with_input(||panic!("shutdown cannot be undone by a stale callback"));
|
||||||
|
let later=input.desktop_session();later.connected();
|
||||||
|
later.with_input(||panic!("shutdown must reject newly arriving offers too"));
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn input_gate_serializes_disconnect_against_inflight_event() {
|
||||||
|
let input=Arc::new(InputInjector::new());
|
||||||
|
let session=input.desktop_session();session.connected();
|
||||||
|
let (started,ready)=std::sync::mpsc::channel();
|
||||||
|
let (finish,resume)=std::sync::mpsc::channel();
|
||||||
|
let worker=session.clone();
|
||||||
|
let event=std::thread::spawn(move ||worker.with_input(|| {
|
||||||
|
started.send(()).unwrap();resume.recv().unwrap();
|
||||||
|
worker.input.held.lock().unwrap().buttons.insert(1);
|
||||||
|
}));
|
||||||
|
ready.recv().unwrap();
|
||||||
|
let ending=session.clone();
|
||||||
|
let teardown=std::thread::spawn(move ||ending.disconnected());
|
||||||
|
finish.send(()).unwrap();event.join().unwrap();teardown.join().unwrap();
|
||||||
|
assert!(input.held.lock().unwrap().buttons.is_empty());
|
||||||
|
session.with_input(||panic!("late event after disconnect"));
|
||||||
|
}
|
||||||
|
}
|
||||||
+50
@@ -0,0 +1,50 @@
|
|||||||
|
//! Experimental linear KMS framebuffer capture. Never selected automatically.
|
||||||
|
#[cfg(all(feature="kms",target_os="linux"))]
|
||||||
|
mod imp {
|
||||||
|
use std::ffi::CString;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
extern "C" {
|
||||||
|
fn uu_kms_open(device:*const libc::c_char,w:*mut u32,h:*mut u32)->*mut libc::c_void;
|
||||||
|
fn uu_kms_read(handle:*mut libc::c_void,out:*mut u8,length:usize)->i32;
|
||||||
|
fn uu_kms_close(handle:*mut libc::c_void);
|
||||||
|
}
|
||||||
|
struct Handle(*mut libc::c_void);
|
||||||
|
// The FD/CRTC handle is exclusively accessed through Capture's mutex.
|
||||||
|
unsafe impl Send for Handle {}
|
||||||
|
impl Drop for Handle { fn drop(&mut self) { unsafe { uu_kms_close(self.0) }; } }
|
||||||
|
pub struct Capture { handle:Mutex<Handle>, pub width:u32, pub height:u32 }
|
||||||
|
impl Capture {
|
||||||
|
pub fn open()->Result<Self,String> {
|
||||||
|
let device=CString::new(std::env::var("UU_KMS_DEVICE").unwrap_or_else(|_|"/dev/dri/card0".into())).map_err(|_|"Invalid KMS device")?;
|
||||||
|
let (mut width,mut height)=(0,0);
|
||||||
|
let handle=unsafe { uu_kms_open(device.as_ptr(),&mut width,&mut height) };
|
||||||
|
if handle.is_null() { return Err("KMS: cannot open an active CRTC; check card permissions and connected output".into()); }
|
||||||
|
let capture=Self { handle:Mutex::new(Handle(handle)), width,height };
|
||||||
|
// Do not advertise a desktop until we have actually read the buffer.
|
||||||
|
let mut frame=vec![0; width as usize*height as usize*4];
|
||||||
|
capture.capture(&mut frame)?;
|
||||||
|
if frame.chunks_exact(4).all(|p| p[..3]==[0,0,0]) {
|
||||||
|
log::warn!("KMS initial frame is uniformly black; GPU buffer visibility is unverified (see KMS limitations)");
|
||||||
|
}
|
||||||
|
Ok(capture)
|
||||||
|
}
|
||||||
|
pub fn capture(&self,output:&mut [u8])->Result<(),String> {
|
||||||
|
if output.len()!=self.width as usize*self.height as usize*4 { return Err("KMS: invalid frame dimensions".into()); }
|
||||||
|
let handle=self.handle.lock().unwrap();
|
||||||
|
match unsafe { uu_kms_read(handle.0,output.as_mut_ptr(),output.len()) } {
|
||||||
|
0=>Ok(()), -2=>Err("KMS mode changed; restart host to refresh desktop geometry".into()),
|
||||||
|
-5=>Err("KMS framebuffer handles unavailable; requires cap_sys_admin+ep".into()),
|
||||||
|
code=>Err(format!("KMS capture failed ({code}); prototype needs a mappable linear XRGB/ARGB dumb buffer")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[cfg(all(feature="kms",target_os="linux"))]
|
||||||
|
pub use imp::Capture;
|
||||||
|
#[cfg(not(all(feature="kms",target_os="linux")))]
|
||||||
|
pub struct Capture { pub width:u32, pub height:u32 }
|
||||||
|
#[cfg(not(all(feature="kms",target_os="linux")))]
|
||||||
|
impl Capture {
|
||||||
|
pub fn open()->Result<Self,String> { Err("KMS requires Linux and --features kms".into()) }
|
||||||
|
pub fn capture(&self,_:&mut [u8])->Result<(),String> { Err("KMS requires Linux and --features kms".into()) }
|
||||||
|
}
|
||||||
+26
-7
@@ -143,7 +143,7 @@ fn qr_rgba(content: &str, scale: usize, quiet: usize) -> Result<(Vec<u8>, usize)
|
|||||||
Ok((rgba, edge))
|
Ok((rgba, edge))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_config(cfg: &Value) -> Result<std::path::PathBuf, String> {
|
pub(crate) fn write_config(cfg: &Value) -> Result<std::path::PathBuf, String> {
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
|
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
|
||||||
let path = crate::config::path();
|
let path = crate::config::path();
|
||||||
@@ -227,8 +227,8 @@ pub async fn run(gui: bool) -> Result<(), String> {
|
|||||||
result.map(|_| ())
|
result.map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the account nickname (or user id) on success.
|
/// Local installation identifiers only; guest creation does not require device/init.
|
||||||
async fn flow(ui: &LoginUi) -> Result<String, String> {
|
pub fn prepare_identity() -> Result<(Value, Credentials), String> {
|
||||||
let mut cfg = crate::config::file()?.cloned().unwrap_or_else(|| json!({}));
|
let mut cfg = crate::config::file()?.cloned().unwrap_or_else(|| json!({}));
|
||||||
let get = |cfg: &Value, k: &str| std::env::var(format!("UU_{}", k.to_uppercase())).ok().filter(|v| !v.is_empty())
|
let get = |cfg: &Value, k: &str| std::env::var(format!("UU_{}", k.to_uppercase())).ok().filter(|v| !v.is_empty())
|
||||||
.or_else(|| cfg.get(k).and_then(|v| v.as_str()).map(str::to_owned)).unwrap_or_default();
|
.or_else(|| cfg.get(k).and_then(|v| v.as_str()).map(str::to_owned)).unwrap_or_default();
|
||||||
@@ -237,8 +237,16 @@ async fn flow(ui: &LoginUi) -> Result<String, String> {
|
|||||||
for (key, make) in [("client_id", random_uuid as fn() -> String), ("system_id", || random_uuid().to_uppercase()), ("machine_guid", machine_guid)] {
|
for (key, make) in [("client_id", random_uuid as fn() -> String), ("system_id", || random_uuid().to_uppercase()), ("machine_guid", machine_guid)] {
|
||||||
if get(&cfg, key).is_empty() { cfg[key] = json!(make()); }
|
if get(&cfg, key).is_empty() { cfg[key] = json!(make()); }
|
||||||
}
|
}
|
||||||
let mut creds = Credentials { client_id: get(&cfg, "client_id"), system_id: get(&cfg, "system_id"), sign_key, ..Default::default() };
|
let creds = Credentials { client_id: get(&cfg, "client_id"), system_id: get(&cfg, "system_id"), device_id: get(&cfg, "device_id"), sign_key, ..Default::default() };
|
||||||
|
|
||||||
|
write_config(&cfg)?;
|
||||||
|
Ok((cfg, creds))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn prepare_device() -> Result<(Value, Credentials), String> {
|
||||||
|
let (mut cfg, mut creds) = prepare_identity()?;
|
||||||
|
let get = |cfg: &Value, k: &str| std::env::var(format!("UU_{}", k.to_uppercase())).ok().filter(|v| !v.is_empty())
|
||||||
|
.or_else(|| cfg.get(k).and_then(|v| v.as_str()).map(str::to_owned)).unwrap_or_default();
|
||||||
if get(&cfg, "device_id").is_empty() {
|
if get(&cfg, "device_id").is_empty() {
|
||||||
let body = json!({
|
let body = json!({
|
||||||
"name": hostname(), "client_id": creds.client_id, "system_id": creds.system_id, "machine_guid": get(&cfg, "machine_guid"),
|
"name": hostname(), "client_id": creds.client_id, "system_id": creds.system_id, "machine_guid": get(&cfg, "machine_guid"),
|
||||||
@@ -248,11 +256,17 @@ async fn flow(ui: &LoginUi) -> Result<String, String> {
|
|||||||
let (status, resp) = UUAuthClient::new(creds.clone()).post_json("/api/v1/device/windows/init", &body).await?;
|
let (status, resp) = UUAuthClient::new(creds.clone()).post_json("/api/v1/device/windows/init", &body).await?;
|
||||||
let d = data("device init", status, &resp)?;
|
let d = data("device init", status, &resp)?;
|
||||||
let id = d.get("device_id").and_then(|v| v.as_str()).ok_or("device init returned no device_id")?;
|
let id = d.get("device_id").and_then(|v| v.as_str()).ok_or("device init returned no device_id")?;
|
||||||
println!("Registered device: name={} id={}", d.get("alias").and_then(|v| v.as_str()).unwrap_or(""), id);
|
log::info!("Registered local device identity");
|
||||||
cfg["device_id"] = json!(id);
|
cfg["device_id"] = json!(id);
|
||||||
write_config(&cfg)?; // keep the device even if the QR step is abandoned
|
write_config(&cfg)?; // keep the device even if the QR step is abandoned
|
||||||
}
|
}
|
||||||
creds.device_id = get(&cfg, "device_id");
|
creds.device_id = get(&cfg, "device_id");
|
||||||
|
Ok((cfg, creds))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the account nickname (or user id) on success.
|
||||||
|
pub(crate) async fn flow(ui: &LoginUi) -> Result<String, String> {
|
||||||
|
let (mut cfg, creds) = prepare_device().await?;
|
||||||
let client = UUAuthClient::new(creds.clone());
|
let client = UUAuthClient::new(creds.clone());
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@@ -274,7 +288,7 @@ async fn flow(ui: &LoginUi) -> Result<String, String> {
|
|||||||
let mut file=std::fs::OpenOptions::new().write(true).create(true).truncate(true).mode(0o600).open(path).map_err(|e|e.to_string())?;
|
let mut file=std::fs::OpenOptions::new().write(true).create(true).truncate(true).mode(0o600).open(path).map_err(|e|e.to_string())?;
|
||||||
file.write_all(&crate::cursor::encode_png(edge as u32,edge as u32,&rgba)).map_err(|e|e.to_string())?;
|
file.write_all(&crate::cursor::encode_png(edge as u32,edge as u32,&rgba)).map_err(|e|e.to_string())?;
|
||||||
}
|
}
|
||||||
println!("\nScan with the UU Remote mobile app (网易UU远程 App 扫码登录):\n{}", render_qr(&url)?);
|
if !ui.quiet() { println!("\nScan with the UU Remote mobile app (网易UU远程 App 扫码登录):\n{}", render_qr(&url)?); }
|
||||||
// Status is a long poll: send the state already shown (server enum 1..4; observed:
|
// Status is a long poll: send the state already shown (server enum 1..4; observed:
|
||||||
// 1 waiting, 2 scanned, 4 after the phone confirmed); the server answers when it changes.
|
// 1 waiting, 2 scanned, 4 after the phone confirmed); the server answers when it changes.
|
||||||
let mut login_status = 1i64;
|
let mut login_status = 1i64;
|
||||||
@@ -315,8 +329,13 @@ async fn flow(ui: &LoginUi) -> Result<String, String> {
|
|||||||
cfg["token"] = json!(token);
|
cfg["token"] = json!(token);
|
||||||
cfg["user_id"] = json!(user);
|
cfg["user_id"] = json!(user);
|
||||||
cfg["csrf"] = json!(csrf_from_token(token).ok_or("token carries no csrf claim")?);
|
cfg["csrf"] = json!(csrf_from_token(token).ok_or("token carries no csrf claim")?);
|
||||||
let path = write_config(&cfg)?;
|
|
||||||
let nickname = d.get("nickname").and_then(|v| v.as_str()).unwrap_or("");
|
let nickname = d.get("nickname").and_then(|v| v.as_str()).unwrap_or("");
|
||||||
|
if !nickname.is_empty() { cfg["account_name"] = json!(nickname); }
|
||||||
|
// Keep settings saved meanwhile (e.g. from the assist card): merge into the fresh file.
|
||||||
|
if let Ok(Some(Value::Object(fresh))) = crate::config::read_fresh() {
|
||||||
|
for (k, v) in fresh { if cfg.get(&k).is_none() { cfg[k] = v; } }
|
||||||
|
}
|
||||||
|
let path = write_config(&cfg)?;
|
||||||
println!("Logged in as {} ({}); saved to {}", nickname, user, path.display());
|
println!("Logged in as {} ({}); saved to {}", nickname, user, path.display());
|
||||||
return Ok(if nickname.is_empty() { user.to_string() } else { nickname.to_string() });
|
return Ok(if nickname.is_empty() { user.to_string() } else { nickname.to_string() });
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-4
@@ -15,13 +15,13 @@ extern "C" {
|
|||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub enum Tone { Info = 0, Ok = 1, Error = 2 }
|
pub enum Tone { Info = 0, Ok = 1, Error = 2 }
|
||||||
|
|
||||||
pub struct LoginUi { shown: bool }
|
pub struct LoginUi { shown: bool, attached: bool }
|
||||||
|
|
||||||
#[cfg(uu_gtk)]
|
#[cfg(uu_gtk)]
|
||||||
fn cstr(s: &str) -> std::ffi::CString { std::ffi::CString::new(s.replace('\0', "")).unwrap_or_default() }
|
fn cstr(s: &str) -> std::ffi::CString { std::ffi::CString::new(s.replace('\0', "")).unwrap_or_default() }
|
||||||
|
|
||||||
impl LoginUi {
|
impl LoginUi {
|
||||||
pub fn none() -> Self { LoginUi { shown: false } }
|
pub fn none() -> Self { LoginUi { shown: false, attached: false } }
|
||||||
|
|
||||||
/// Opens the window when built with GTK and a display is present; falls back silently otherwise.
|
/// Opens the window when built with GTK and a display is present; falls back silently otherwise.
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
@@ -30,13 +30,23 @@ impl LoginUi {
|
|||||||
if std::env::var_os("DISPLAY").is_some() || std::env::var_os("WAYLAND_DISPLAY").is_some() {
|
if std::env::var_os("DISPLAY").is_some() || std::env::var_os("WAYLAND_DISPLAY").is_some() {
|
||||||
let (n, o, c, g) = (cstr(name), cstr(os), cstr(cpu), cstr(gpu));
|
let (n, o, c, g) = (cstr(name), cstr(os), cstr(cpu), cstr(gpu));
|
||||||
if unsafe { uu_login_ui_start(n.as_ptr(), o.as_ptr(), c.as_ptr(), g.as_ptr()) } == 0 {
|
if unsafe { uu_login_ui_start(n.as_ptr(), o.as_ptr(), c.as_ptr(), g.as_ptr()) } == 0 {
|
||||||
return LoginUi { shown: true };
|
return LoginUi { shown: true, attached: false };
|
||||||
}
|
}
|
||||||
eprintln!("Login window unavailable; using the terminal QR code");
|
eprintln!("Login window unavailable; using the terminal QR code");
|
||||||
}
|
}
|
||||||
LoginUi::none()
|
LoginUi::none()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Account card of the host's own main window (already open): the QR stays in the window only
|
||||||
|
/// (a service's stdout is the journal and the QR URL is a login ticket), and dropping this
|
||||||
|
/// handle never closes the shared window.
|
||||||
|
pub fn attached() -> Self {
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
{ unsafe { uu_login_ui_start(std::ptr::null(), std::ptr::null(), std::ptr::null(), std::ptr::null()) }; LoginUi { shown: true, attached: true } }
|
||||||
|
#[cfg(not(uu_gtk))]
|
||||||
|
LoginUi { shown: false, attached: true }
|
||||||
|
}
|
||||||
|
pub fn quiet(&self) -> bool { self.attached }
|
||||||
pub fn shown(&self) -> bool { self.shown }
|
pub fn shown(&self) -> bool { self.shown }
|
||||||
|
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
@@ -73,6 +83,6 @@ impl LoginUi {
|
|||||||
impl Drop for LoginUi {
|
impl Drop for LoginUi {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
#[cfg(uu_gtk)]
|
#[cfg(uu_gtk)]
|
||||||
if self.shown { unsafe { uu_login_ui_close() } }
|
if self.shown && !self.attached { unsafe { uu_login_ui_close() } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+374
-54
@@ -7,22 +7,30 @@ mod network_diagnostics;
|
|||||||
mod session_ice;
|
mod session_ice;
|
||||||
mod wayland;
|
mod wayland;
|
||||||
mod auth;
|
mod auth;
|
||||||
|
mod assist;
|
||||||
mod config;
|
mod config;
|
||||||
mod login;
|
mod login;
|
||||||
mod login_ui;
|
mod login_ui;
|
||||||
mod session_lock;
|
mod session_lock;
|
||||||
mod capture;
|
mod capture;
|
||||||
|
mod kms;
|
||||||
mod file_trans;
|
mod file_trans;
|
||||||
mod input;
|
mod input;
|
||||||
|
mod uinput;
|
||||||
mod input_mapping;
|
mod input_mapping;
|
||||||
mod port_forward;
|
mod port_mapping;
|
||||||
mod power;
|
mod power;
|
||||||
mod signaling;
|
mod signaling;
|
||||||
mod terminal;
|
mod terminal;
|
||||||
mod webrtc;
|
mod webrtc;
|
||||||
mod video;
|
mod video;
|
||||||
|
#[cfg(feature = "inproc-av")]
|
||||||
|
mod damage_capture;
|
||||||
|
#[cfg(feature = "inproc-av")]
|
||||||
|
mod inproc_encoder;
|
||||||
mod capture_setting;
|
mod capture_setting;
|
||||||
mod audio;
|
mod audio;
|
||||||
|
mod virtual_audio;
|
||||||
mod system_actions;
|
mod system_actions;
|
||||||
mod capability;
|
mod capability;
|
||||||
mod protocol;
|
mod protocol;
|
||||||
@@ -46,18 +54,103 @@ use std::sync::Arc;
|
|||||||
use tokio::sync::{mpsc, Mutex};
|
use tokio::sync::{mpsc, Mutex};
|
||||||
use webrtc::WebRtcSession;
|
use webrtc::WebRtcSession;
|
||||||
|
|
||||||
|
/// Set before a normal return when the host must come back in the other mode (signed in / out).
|
||||||
|
static RESTART: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
|
||||||
|
/// Re-executes this host; the new instance opens its window so the user sees the result.
|
||||||
|
fn restart_host() -> ! {
|
||||||
|
use std::os::unix::process::CommandExt;
|
||||||
|
let error = std::process::Command::new("/proc/self/exe").args(std::env::args_os().skip(1)).env("UU_SHOW_WINDOW", "1").exec();
|
||||||
|
eprintln!("restart failed: {error}");
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
/// systemd user unit running this process (from /proc/self/cgroup), e.g. "uuremote-native.service".
|
||||||
|
fn systemd_unit() -> Option<String> {
|
||||||
|
std::env::var_os("INVOCATION_ID")?;
|
||||||
|
let cgroup = std::fs::read_to_string("/proc/self/cgroup").ok()?;
|
||||||
|
cgroup.lines().find_map(|l| l.rsplit('/').next().filter(|u| u.ends_with(".service")).map(str::to_owned))
|
||||||
|
}
|
||||||
|
fn display_present() -> bool { std::env::var_os("DISPLAY").is_some() || std::env::var_os("WAYLAND_DISPLAY").is_some() }
|
||||||
|
/// SIGUSR1 (`uuremote-server show`) opens the host window.
|
||||||
|
fn present_on_usr1(ui: std::sync::Arc<assist::LocalUi>) {
|
||||||
|
// Install the handler before publishing the pid: SIGUSR1's default action terminates the process.
|
||||||
|
let Ok(mut usr1) = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::user_defined1()) else { return };
|
||||||
|
write_host_pid();
|
||||||
|
tokio::spawn(async move { while usr1.recv().await.is_some() { ui.present(); } });
|
||||||
|
}
|
||||||
|
|
||||||
|
fn host_pid_path() -> std::path::PathBuf { config::path().with_file_name("host.pid") }
|
||||||
|
/// Lets `uuremote-server show` find this host service to open its window (SIGUSR1).
|
||||||
|
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 _ = write!(f, "{}", std::process::id());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// `uuremote-server show` (desktop launcher): open the running host's window, or start the host in the
|
||||||
|
/// foreground with its window when none runs (e.g. no systemd unit installed yet).
|
||||||
|
fn show_running_host() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let running = std::fs::read_to_string(host_pid_path()).ok().and_then(|s| s.trim().parse::<i32>().ok()).filter(|p| *p > 1)
|
||||||
|
.filter(|pid| {
|
||||||
|
let exe = std::fs::read_link(format!("/proc/{pid}/exe")).ok();
|
||||||
|
let own = std::env::current_exe().ok();
|
||||||
|
exe.is_some() && exe.as_ref().and_then(|p| p.file_name()) == own.as_ref().and_then(|p| p.file_name())
|
||||||
|
});
|
||||||
|
if let Some(pid) = running {
|
||||||
|
if unsafe { libc::kill(pid, libc::SIGUSR1) } == 0 { return Ok(()); }
|
||||||
|
}
|
||||||
|
// Stopped from the tray but installed as a user service: start the service (not a second,
|
||||||
|
// unmanaged host), wait for it to publish its pid, then ask it to show the window.
|
||||||
|
let unit = std::env::var("UU_SERVICE_UNIT").unwrap_or_else(|_| "uuremote-native.service".into());
|
||||||
|
let installed = std::process::Command::new("systemctl").args(["--user", "cat", &unit])
|
||||||
|
.stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null()).status().is_ok_and(|s| s.success());
|
||||||
|
if installed && std::process::Command::new("systemctl").args(["--user", "start", &unit]).status().is_ok_and(|s| s.success()) {
|
||||||
|
let before = std::fs::metadata(host_pid_path()).and_then(|m| m.modified()).ok();
|
||||||
|
for _ in 0..60 {
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(500));
|
||||||
|
let fresh = std::fs::metadata(host_pid_path()).and_then(|m| m.modified()).ok().is_some_and(|t| Some(t) != before);
|
||||||
|
let pid = std::fs::read_to_string(host_pid_path()).ok().and_then(|s| s.trim().parse::<i32>().ok());
|
||||||
|
if let (true, Some(pid)) = (fresh, pid) {
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(500)); // signal handler installed right after the pid
|
||||||
|
if unsafe { libc::kill(pid, libc::SIGUSR1) } == 0 { return Ok(()); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Err("the UU remote host service started but did not open its window".into());
|
||||||
|
}
|
||||||
|
use std::os::unix::process::CommandExt;
|
||||||
|
Err(Box::new(std::process::Command::new("/proc/self/exe").env("UU_SHOW_WINDOW", "1").exec()))
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
|
// Not signed in, with a desktop: one window offers remote assistance (guest identity) and the QR
|
||||||
|
// login together; signing in restarts the host in account mode (assistance continues there).
|
||||||
|
let unified = std::env::args().nth(1).is_none() && display_present()
|
||||||
|
&& std::env::var("UU_REMOTE_ASSIST").ok().as_deref() != Some("0") && std::env::var("UU_LOGIN_WINDOW").ok().as_deref() != Some("0")
|
||||||
|
&& config::read_fresh().ok().flatten().and_then(|c| c.get("token").and_then(|t| t.as_str()).map(|t| !t.is_empty())) != Some(true)
|
||||||
|
&& ["UU_USER_ID", "UU_TOKEN", "UU_CSRF"].iter().all(|k| std::env::var_os(k).map_or(true, |v| v.is_empty()));
|
||||||
|
let assist_enabled = unified || std::env::args().nth(1).as_deref() == Some("assist")
|
||||||
|
|| (std::env::args().nth(1).is_none() && std::env::var("UU_REMOTE_ASSIST").ok().as_deref() == Some("1"));
|
||||||
|
if assist_enabled {
|
||||||
|
// HTTP/WS dependency trace logging can expose Authorization or room headers.
|
||||||
|
env_logger::Builder::new().filter_level(log::LevelFilter::Off)
|
||||||
|
.filter_module("uuremote_server", log::LevelFilter::Info).init();
|
||||||
|
} else { env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init(); }
|
||||||
// An unknown argument must never fall through to starting a second full host server.
|
// An unknown argument must never fall through to starting a second full host server.
|
||||||
const SUBCOMMANDS: [&str; 8] = ["login", "unlock-check", "desktop-probe", "desktop-video-probe", "wayland-capture",
|
const SUBCOMMANDS: [&str; 13] = ["show", "assist", "desktop-input-probe", "virtual-audio-probe", "login", "unlock-check", "desktop-probe", "desktop-video-probe", "desktop-audio-probe", "wayland-capture",
|
||||||
"wayland-input-probe", "clipboard-acceptance", "portal-acceptance"];
|
"wayland-input-probe", "clipboard-acceptance", "portal-acceptance"];
|
||||||
if let Some(arg) = std::env::args().nth(1).filter(|a| !SUBCOMMANDS.contains(&a.as_str())) {
|
if let Some(arg) = std::env::args().nth(1).filter(|a| !SUBCOMMANDS.contains(&a.as_str())) {
|
||||||
let usage = format!("usage: uuremote-server [{}]\n (no argument: run the host)", SUBCOMMANDS.join(" | "));
|
let usage = format!("usage: uuremote-server [{}]\n (no argument: run the host)", SUBCOMMANDS.join(" | "));
|
||||||
if matches!(arg.as_str(), "help" | "-h" | "--help") { println!("{usage}"); return Ok(()); }
|
if matches!(arg.as_str(), "help" | "-h" | "--help") { println!("{usage}"); return Ok(()); }
|
||||||
eprintln!("unknown argument: {arg}\n{usage}");
|
eprintln!("unknown argument\n{usage}");
|
||||||
std::process::exit(2);
|
std::process::exit(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if std::env::args().nth(1).as_deref() == Some("show") { return show_running_host(); }
|
||||||
|
if assist_enabled && !unified {
|
||||||
|
if std::env::args().len() > 2 { return Err("assist accepts no additional arguments; configure it with a private UU_CONFIG".into()); }
|
||||||
|
assist::configure()?;
|
||||||
|
}
|
||||||
if std::env::args().nth(1).as_deref() == Some("clipboard-acceptance") {
|
if std::env::args().nth(1).as_deref() == Some("clipboard-acceptance") {
|
||||||
return tokio::task::spawn_blocking(clipboard_probe::run).await?.map_err(Into::into);
|
return tokio::task::spawn_blocking(clipboard_probe::run).await?.map_err(Into::into);
|
||||||
}
|
}
|
||||||
@@ -159,6 +252,48 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let path=std::env::args().nth(2);
|
let path=std::env::args().nth(2);
|
||||||
return video::probe((screen.width,screen.height),screen.display.clone(),screen.portal.clone(),path).await.map_err(|e|e.into());
|
return video::probe((screen.width,screen.height),screen.display.clone(),screen.portal.clone(),path).await.map_err(|e|e.into());
|
||||||
}
|
}
|
||||||
|
if std::env::args().nth(1).as_deref() == Some("desktop-input-probe") {
|
||||||
|
if std::fs::read_to_string("/proc/sys/kernel/hostname").unwrap_or_default().trim()!="uu-hyprland-test" {
|
||||||
|
return Err("Input acceptance is restricted to the isolated VM".into());
|
||||||
|
}
|
||||||
|
let screen=ScreenCapture::new()?;
|
||||||
|
let input=Arc::new(InputInjector::new());
|
||||||
|
input.bind_portal(screen.portal.clone());
|
||||||
|
let worker=input.start_pointer_worker();
|
||||||
|
// libinput discovers newly created uinput devices asynchronously.
|
||||||
|
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||||
|
input.mouse_absolute_normalized(0.25,0.25,(screen.width,screen.height));
|
||||||
|
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||||
|
input.mouse_absolute_normalized(0.5,0.5,(screen.width,screen.height));
|
||||||
|
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||||
|
input.mouse_button(1,true); input.mouse_button(1,false);
|
||||||
|
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||||
|
let measure=std::env::var("UU_INPUT_MEASURE").is_ok_and(|v|v=="1");
|
||||||
|
let cursor=|| -> Result<serde_json::Value,Box<dyn std::error::Error>> {
|
||||||
|
let output=std::process::Command::new("hyprctl").args(["-j","cursorpos"]).output()?;
|
||||||
|
if !output.status.success() { return Err("Cursor measurement unavailable".into()); }
|
||||||
|
Ok(serde_json::from_slice(&output.stdout)?)
|
||||||
|
};
|
||||||
|
let before=if measure {Some(cursor()?)} else {None};
|
||||||
|
input.mouse_relative(32,0);
|
||||||
|
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
|
||||||
|
if let Some(before)=before {
|
||||||
|
println!("Relative pointer measurement: {}",serde_json::json!({"before":before,"after":cursor()?,"requested_delta":[32,0]}));
|
||||||
|
}
|
||||||
|
for key in b"UUWAYLAND" { input.windows_key_event(i32::from(*key),true); input.windows_key_event(i32::from(*key),false); }
|
||||||
|
if std::env::var("UU_INPUT_SYMBOLS").is_ok_and(|v|v=="1") {
|
||||||
|
// Fixed synthetic symbols exercise the same VK+modifier path as CONTROL.
|
||||||
|
for (key,shift) in [(0xbd,true),(0xbb,true),(0xbd,false)] {
|
||||||
|
if shift {input.windows_key_event(0x10,true);}
|
||||||
|
input.windows_key_event(key,true); input.windows_key_event(key,false);
|
||||||
|
if shift {input.windows_key_event(0x10,false);}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
|
||||||
|
worker.abort(); input.release_held();
|
||||||
|
println!("Desktop test events sent; verify the GTK acceptance report");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
if std::env::args().nth(1).as_deref() == Some("wayland-input-probe") {
|
if std::env::args().nth(1).as_deref() == Some("wayland-input-probe") {
|
||||||
return wayland::input_probe().map_err(|e|e.into());
|
return wayland::input_probe().map_err(|e|e.into());
|
||||||
}
|
}
|
||||||
@@ -185,9 +320,22 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
capture::keep_display_awake()?;
|
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());
|
return video::probe((screen.width,screen.height),screen.display.clone(),screen.portal.clone(),std::env::args().nth(2)).await.map_err(|e|e.into());
|
||||||
}
|
}
|
||||||
|
if std::env::args().nth(1).as_deref() == Some("desktop-audio-probe") {
|
||||||
|
return audio::probe(std::env::args().nth(2).and_then(|v| v.parse().ok()).unwrap_or(5)).await.map_err(|e| e.into());
|
||||||
|
}
|
||||||
if std::env::args().nth(1).as_deref() == Some("unlock-check") {
|
if std::env::args().nth(1).as_deref() == Some("unlock-check") {
|
||||||
return session_lock::check_from_stdin().map_err(|e| e.into());
|
return session_lock::check_from_stdin().map_err(|e| e.into());
|
||||||
}
|
}
|
||||||
|
if std::env::args().nth(1).as_deref() == Some("virtual-audio-probe") {
|
||||||
|
virtual_audio::init();
|
||||||
|
if !virtual_audio::enable() { return Err("Virtual audio probe setup failed".into()); }
|
||||||
|
println!("Virtual audio probe active");
|
||||||
|
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
|
||||||
|
virtual_audio::disable();
|
||||||
|
if virtual_audio::active() { return Err("Virtual audio probe restore incomplete".into()); }
|
||||||
|
println!("Virtual audio probe ended");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
if std::env::args().nth(1).as_deref() == Some("login") {
|
if std::env::args().nth(1).as_deref() == Some("login") {
|
||||||
// Interactive: register this host and bind it to an account via QR scan
|
// Interactive: register this host and bind it to an account via QR scan
|
||||||
// (GTK window when available; `login --no-gui` keeps it terminal-only).
|
// (GTK window when available; `login --no-gui` keeps it terminal-only).
|
||||||
@@ -195,9 +343,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
if login::run(gui).await.is_err() { std::process::exit(1); } // error already shown
|
if login::run(gui).await.is_err() { std::process::exit(1); } // error already shown
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
virtual_audio::init();
|
||||||
|
let _audio_cleanup=virtual_audio::Cleanup;
|
||||||
log::info!("Starting uu-remote-linux host v{}...", env!("CARGO_PKG_VERSION"));
|
log::info!("Starting uu-remote-linux host v{}...", env!("CARGO_PKG_VERSION"));
|
||||||
|
|
||||||
let mut capture = Arc::new((if desktop_backend::detect()?==desktop_backend::DesktopBackend::Portal
|
let capture = Arc::new((if desktop_backend::headless() {
|
||||||
|
Err("headless mode (UU_DESKTOP_BACKEND=none)".to_owned())
|
||||||
|
} else if desktop_backend::detect()?==desktop_backend::DesktopBackend::Portal
|
||||||
&& std::env::var("UU_PORTAL_PREAUTHORIZE").ok().as_deref()!=Some("1") {
|
&& std::env::var("UU_PORTAL_PREAUTHORIZE").ok().as_deref()!=Some("1") {
|
||||||
Err("Portal screen access requires a desktop connection and local consent".to_owned())
|
Err("Portal screen access requires a desktop connection and local consent".to_owned())
|
||||||
} else { ScreenCapture::new() }).unwrap_or_else(|error| {
|
} else { ScreenCapture::new() }).unwrap_or_else(|error| {
|
||||||
@@ -205,34 +357,125 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
ScreenCapture::unavailable(error)
|
ScreenCapture::unavailable(error)
|
||||||
}));
|
}));
|
||||||
let input = Arc::new(InputInjector::new());
|
let input = Arc::new(InputInjector::new());
|
||||||
|
input.bind_portal(capture.portal.clone());
|
||||||
|
input.release_held(); // recover stale input before accepting any peer
|
||||||
let _pointer_worker = input.start_pointer_worker();
|
let _pointer_worker = input.start_pointer_worker();
|
||||||
#[cfg(target_os="linux")]
|
let (shutdown_tx, mut shutdown) = tokio::sync::watch::channel(false);
|
||||||
{
|
let shutdown_input = input.clone();
|
||||||
let shutdown_input=input.clone();
|
let signal_tx = shutdown_tx.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
#[cfg(target_os="linux")]
|
||||||
|
{
|
||||||
|
let mut term = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()).expect("SIGTERM handler");
|
||||||
|
tokio::select! { _ = term.recv() => {}, _ = tokio::signal::ctrl_c() => {} }
|
||||||
|
}
|
||||||
|
#[cfg(not(target_os="linux"))]
|
||||||
|
let _ = tokio::signal::ctrl_c().await;
|
||||||
|
if !assist_enabled { shutdown_input.shutdown(); virtual_audio::disable(); terminal::shutdown_all(); std::process::exit(0); }
|
||||||
|
let _ = signal_tx.send(true);
|
||||||
|
});
|
||||||
|
let mut assist = if assist_enabled { Some(assist::Assist::start(&mut shutdown, unified).await?) } else { None };
|
||||||
|
if let Some(session) = assist.as_ref().filter(|_| unified) {
|
||||||
|
present_on_usr1(session.ui());
|
||||||
|
let login_done = shutdown_tx.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
if let Ok(mut term)=tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
|
let ui = login_ui::LoginUi::attached();
|
||||||
term.recv().await; shutdown_input.release_held(); terminal::shutdown_all(); std::process::exit(0);
|
loop {
|
||||||
|
match login::flow(&ui).await {
|
||||||
|
Ok(_) => { RESTART.store(true, std::sync::atomic::Ordering::SeqCst); let _ = login_done.send(true); return; }
|
||||||
|
Err(error) => { log::warn!("QR login failed: {error}; retrying"); tokio::time::sleep(std::time::Duration::from_secs(5)).await; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
let creds = match config::file().and_then(Credentials::load) {
|
let webrtc_sessions: Arc<Mutex<HashMap<String, WebRtcSession>>> = Arc::new(Mutex::new(HashMap::new()));
|
||||||
Ok(creds) => creds,
|
let (mut commands, command_task) = match assist.as_ref() {
|
||||||
Err(e) => {
|
Some(s) => { let (rx, task) = s.commands(shutdown_tx); (rx, Some(task)) },
|
||||||
log::warn!("Not logged in: {e} ({} or UU_* environment variables)", config::path().display());
|
None => (tokio::sync::mpsc::channel(1).1, None),
|
||||||
login::wait_for_credentials(None).await
|
};
|
||||||
|
let mut immediate_shutdown = shutdown.clone();
|
||||||
|
let result = tokio::select! {
|
||||||
|
result = serve(capture, input.clone(), &mut shutdown, &mut assist, &mut commands, webrtc_sessions.clone()) => result,
|
||||||
|
_ = immediate_shutdown.changed() => Ok(()),
|
||||||
|
};
|
||||||
|
if let Some(task) = command_task { task.abort(); }
|
||||||
|
// One exit path for commands, signals, transport failures and UI failures.
|
||||||
|
if let Some(session) = assist.as_mut() { session.allowed = false; }
|
||||||
|
for (_, old) in webrtc_sessions.lock().await.drain() { let _ = old.pc.close().await; }
|
||||||
|
virtual_audio::disable(); input.shutdown(); terminal::shutdown_all();
|
||||||
|
// Switching identity (signed in / out) restarts at once and the new identity republishes the same
|
||||||
|
// device share: logout already withdrew it, and after a QR login the cloud moved it to the account
|
||||||
|
// (a guest cancel then fails). Only a real stop withdraws here.
|
||||||
|
let switching = RESTART.load(std::sync::atomic::Ordering::SeqCst);
|
||||||
|
let revoke = match assist.as_mut() { Some(s) if !switching => s.cancel().await, _ => Ok(()) };
|
||||||
|
if revoke.is_err() { log::error!("Cloud assistance revocation failed; host is closed locally"); }
|
||||||
|
result?;
|
||||||
|
if RESTART.load(std::sync::atomic::Ordering::SeqCst) { restart_host(); }
|
||||||
|
revoke.map_err(|_| "assist disabled locally, but cloud cancel failed; cloud revocation is unverified")?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutdown: &mut tokio::sync::watch::Receiver<bool>,
|
||||||
|
assist: &mut Option<assist::Assist>, commands: &mut mpsc::Receiver<assist::Command>, webrtc_sessions: Arc<Mutex<HashMap<String, WebRtcSession>>>) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let mut auth = if let Some(session) = assist.as_ref() { session.client.clone() } else {
|
||||||
|
let creds = match config::file().and_then(Credentials::load) {
|
||||||
|
Ok(creds) => creds,
|
||||||
|
Err(e) => {
|
||||||
|
log::warn!("Not logged in: {e} ({} or UU_* environment variables)", config::path().display());
|
||||||
|
login::wait_for_credentials(None).await
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let client = Arc::new(UUAuthClient::new(creds.clone()));
|
||||||
|
// Logged-in host: remote assistance (ID + code) alongside the device list, like the official
|
||||||
|
// client. Shown in a hidden window opened with `uuremote-server show`; UU_REMOTE_ASSIST=0 disables.
|
||||||
|
if std::env::var("UU_REMOTE_ASSIST").ok().as_deref() != Some("0") {
|
||||||
|
match assist::Assist::start_account(client.clone(), creds) {
|
||||||
|
Ok(session) => {
|
||||||
|
let (rx, _task) = session.commands(tokio::sync::watch::channel(false).0);
|
||||||
|
*commands = rx;
|
||||||
|
session.load_account_name().await;
|
||||||
|
present_on_usr1(session.ui());
|
||||||
|
if std::env::var("UU_SHOW_WINDOW").ok().as_deref() == Some("1") { session.ui().present(); }
|
||||||
|
std::env::remove_var("UU_SHOW_WINDOW");
|
||||||
|
log::info!("Remote assistance ready on the logged-in host (window: uuremote-server show)");
|
||||||
|
*assist = Some(session);
|
||||||
|
}
|
||||||
|
Err(error) => log::info!("Remote assistance unavailable on this host: {error}"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
client
|
||||||
};
|
};
|
||||||
let mut auth = Arc::new(UUAuthClient::new(creds));
|
|
||||||
|
|
||||||
// One peer per viewer connection (desktop, file transfer, terminal...), keyed by ice_id.
|
// One peer per viewer connection (desktop, file transfer, terminal...), keyed by ice_id.
|
||||||
let webrtc_sessions: Arc<Mutex<HashMap<String, WebRtcSession>>> = Arc::new(Mutex::new(HashMap::new()));
|
|
||||||
let mut next_capture_type: Option<(u64,crate::session_ice::SessionIce)> = None;
|
let mut next_capture_type: Option<(u64,crate::session_ice::SessionIce)> = None;
|
||||||
let mut capture_types: HashMap<String, (u64,crate::session_ice::SessionIce)> = HashMap::new();
|
let mut capture_types: HashMap<String, (u64,crate::session_ice::SessionIce)> = HashMap::new();
|
||||||
|
let mut mac_by_ice: HashMap<String, bool> = HashMap::new();
|
||||||
|
let mut next_mac = false;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
if *shutdown.borrow() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
log::info!("Requesting fresh room credentials from NetEase cloud...");
|
log::info!("Requesting fresh room credentials from NetEase cloud...");
|
||||||
let room_data = match auth.create_room().await {
|
let room_result = tokio::select! {
|
||||||
Ok(data) => data,
|
result = async {
|
||||||
|
match assist.as_mut() {
|
||||||
|
Some(session) if !session.is_account() => {
|
||||||
|
let result = session.room().await.map_err(auth::RoomError::Other);
|
||||||
|
auth = session.client.clone(); result
|
||||||
|
}
|
||||||
|
_ => auth.create_room().await,
|
||||||
|
}
|
||||||
|
} => result,
|
||||||
|
_ = shutdown.changed() => return Ok(()),
|
||||||
|
};
|
||||||
|
let room_data = match room_result {
|
||||||
|
Ok(data) => {
|
||||||
|
if let Some(session) = assist.as_mut().filter(|s| s.is_account()) {
|
||||||
|
if let Err(error) = session.ensure_published().await { log::warn!("Remote assistance share info failed: {error}"); }
|
||||||
|
}
|
||||||
|
data
|
||||||
|
}
|
||||||
Err(auth::RoomError::LoginExpired) => {
|
Err(auth::RoomError::LoginExpired) => {
|
||||||
// Like the official client: an expired token needs a new QR login, not retries.
|
// Like the official client: an expired token needs a new QR login, not retries.
|
||||||
log::error!("UU login is invalid or expired; a new QR login is required");
|
log::error!("UU login is invalid or expired; a new QR login is required");
|
||||||
@@ -247,20 +490,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
log::info!(
|
log::info!("Acquired room signaling credentials");
|
||||||
"Acquired room signaling target: {}",
|
|
||||||
room_data.signaling_server
|
|
||||||
);
|
|
||||||
|
|
||||||
let (tx_event, mut rx_event) = mpsc::channel::<SignalingEvent>(64);
|
let (tx_event, mut rx_event) = mpsc::channel::<SignalingEvent>(64);
|
||||||
let tx_timer = tx_event.clone();
|
let tx_timer = tx_event.clone();
|
||||||
let signaling_client = match SignalingClient::connect(
|
let connected = tokio::select! {
|
||||||
&room_data.signaling_server,
|
result = SignalingClient::connect(&room_data.signaling_server, &room_data.token, tx_event) => result,
|
||||||
&room_data.token,
|
_ = shutdown.changed() => return Ok(()),
|
||||||
tx_event,
|
};
|
||||||
)
|
let signaling_client = match connected {
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(cli) => {
|
Ok(cli) => {
|
||||||
let renewal_peers=webrtc_sessions.clone();
|
let renewal_peers=webrtc_sessions.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
@@ -282,27 +520,88 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
Arc::new(cli)
|
Arc::new(cli)
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Failed to connect signaling: {}. Retrying in 5s...", e);
|
let _ = e;
|
||||||
|
log::error!("Failed to connect signaling; retrying in 5s (details omitted)");
|
||||||
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
|
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 事件处理主循环
|
// Guest display/control commands never use stdout, stderr or logs.
|
||||||
while let Some(evt) = rx_event.recv().await {
|
let mut assist_tick = tokio::time::interval(std::time::Duration::from_secs(1));
|
||||||
|
loop {
|
||||||
|
let event = tokio::select! {
|
||||||
|
biased;
|
||||||
|
_ = shutdown.changed() => Some(SignalingEvent::Stop),
|
||||||
|
command = commands.recv(), if assist.is_some() => Some(SignalingEvent::AssistCommand(command.unwrap_or(assist::Command::Stop))),
|
||||||
|
_ = assist_tick.tick(), if assist.is_some() => Some(SignalingEvent::AssistTick),
|
||||||
|
event = rx_event.recv() => event,
|
||||||
|
};
|
||||||
|
let Some(evt) = event else { break; };
|
||||||
match evt {
|
match evt {
|
||||||
|
SignalingEvent::Stop | SignalingEvent::AssistCommand(assist::Command::Stop) => {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
SignalingEvent::AssistCommand(assist::Command::Allow) => { if let Some(s) = assist.as_mut() { s.allow()?; } }
|
||||||
|
SignalingEvent::AssistCommand(assist::Command::Deny) => { if let Some(s) = assist.as_mut() { s.deny()?; } }
|
||||||
|
SignalingEvent::AssistCommand(assist::Command::Verify(v)) => { if let Some(s) = assist.as_mut() { s.set_verify(v)?; } }
|
||||||
|
SignalingEvent::AssistCommand(assist::Command::Refresh(r)) => { if let Some(s) = assist.as_mut() { s.set_refresh(r)?; } }
|
||||||
|
SignalingEvent::AssistCommand(assist::Command::Custom(c)) => { if let Some(s) = assist.as_mut() { s.set_custom(c)?; } }
|
||||||
|
SignalingEvent::AssistCommand(assist::Command::Quit) => {
|
||||||
|
// Tray 退出: under systemd (Restart=always) stop the unit itself, else just exit.
|
||||||
|
log::info!("Quit requested from the tray");
|
||||||
|
if let Some(unit) = systemd_unit() {
|
||||||
|
let _ = std::process::Command::new("systemctl").args(["--user", "stop", "--no-block", &unit]).status();
|
||||||
|
}
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
SignalingEvent::AssistCommand(assist::Command::Logout) => {
|
||||||
|
if let Some(s) = assist.as_mut().filter(|s| s.is_account()) {
|
||||||
|
s.logout().await;
|
||||||
|
RESTART.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SignalingEvent::AssistCommand(assist::Command::Reset) => { if let Some(s) = assist.as_mut() { s.reset()?; } }
|
||||||
|
SignalingEvent::AssistTick => {
|
||||||
|
let mut sessions = webrtc_sessions.lock().await;
|
||||||
|
let ended: Vec<_> = sessions.iter().filter(|(_, s)| matches!(s.pc.connection_state(),
|
||||||
|
::webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Closed |
|
||||||
|
::webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Failed)).map(|(id, _)| id.clone()).collect();
|
||||||
|
let mut desktop_ended = false;
|
||||||
|
for id in ended { if let Some(s) = sessions.remove(&id) { desktop_ended |= s.desktop; let _ = s.pc.close().await; } }
|
||||||
|
if desktop_ended && !sessions.values().any(|s| s.desktop) { if let Some(s) = assist.as_mut() { s.session_ended()?; } }
|
||||||
|
if let Some(s) = assist.as_mut() { s.tick()?; }
|
||||||
|
}
|
||||||
SignalingEvent::Connected => {
|
SignalingEvent::Connected => {
|
||||||
log::info!("Signaling handshake established.");
|
log::info!("Signaling handshake established.");
|
||||||
}
|
}
|
||||||
SignalingEvent::RoomInfoAck(val) => {
|
SignalingEvent::RoomInfoAck(_) => { log::info!("RoomInfo acknowledged by NetEase server"); }
|
||||||
log::info!(
|
|
||||||
"RoomInfo acknowledged by NetEase server: room_id={}",
|
|
||||||
val.get("room_id")
|
|
||||||
.and_then(|v| v.as_str())
|
|
||||||
.unwrap_or("unknown")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
SignalingEvent::DeviceInfoChanged(val) => {
|
SignalingEvent::DeviceInfoChanged(val) => {
|
||||||
|
if assist.is_some() {
|
||||||
|
// Shape only (type value + key names), never field values: guest challenges carry salts/ids.
|
||||||
|
let keys = |v: Option<&serde_json::Value>| v.and_then(|v| v.as_object()).map(|o| o.keys().cloned().collect::<Vec<_>>()).unwrap_or_default();
|
||||||
|
log::debug!("Assist bmsg_push shape: type={:?} keys={:?} data_keys={:?}",
|
||||||
|
val.get("type").and_then(serde_json::Value::as_str), keys(Some(&val)), keys(val.get("data")));
|
||||||
|
}
|
||||||
|
if val.get("type").and_then(serde_json::Value::as_str) == Some("get_control_mode") {
|
||||||
|
if let Some(s) = assist.as_mut() {
|
||||||
|
match s.control_mode(val.get("data").unwrap_or(&serde_json::Value::Null)).await {
|
||||||
|
Ok(()) => log::info!("Assist control mode reported"),
|
||||||
|
Err(error) => log::warn!("Assist control mode report failed: {error}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if val.get("type").and_then(serde_json::Value::as_str) == Some("remote_control") {
|
||||||
|
if let Some(s) = assist.as_mut() {
|
||||||
|
if let Err(error) = s.challenge(val.get("data").unwrap_or(&serde_json::Value::Null)).await {
|
||||||
|
log::warn!("Assist challenge failed: {error}");
|
||||||
|
}
|
||||||
|
if s.renewed { break; }
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
log::info!(
|
log::info!(
|
||||||
"Device status synchronized: status={:?}, availability={:?}",
|
"Device status synchronized: status={:?}, availability={:?}",
|
||||||
val.get("data")
|
val.get("data")
|
||||||
@@ -313,7 +612,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
.and_then(|s| s.as_str())
|
.and_then(|s| s.as_str())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
SignalingEvent::ConnectRequest { capture_type, ice_id, transport } => {
|
SignalingEvent::ConnectRequest { capture_type, ice_id, transport, controller_mac } => {
|
||||||
|
if mac_by_ice.len()>=32 { mac_by_ice.clear(); }
|
||||||
|
match ice_id.clone() { Some(id) => { mac_by_ice.insert(id, controller_mac); } None => next_mac = controller_mac }
|
||||||
// Pending cloud credentials live only within this room; cap
|
// Pending cloud credentials live only within this room; cap
|
||||||
// requests that never receive an offer rather than retain them.
|
// requests that never receive an offer rather than retain them.
|
||||||
if capture_types.len()>=32 { capture_types.clear(); }
|
if capture_types.len()>=32 { capture_types.clear(); }
|
||||||
@@ -325,23 +626,27 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SignalingEvent::Soac(soac) => {
|
SignalingEvent::Soac(soac) => {
|
||||||
log::info!(
|
log::info!("SOAC signaling received: offer={} candidate={}", soac.msg_type == "offer", soac.msg_type == "candidate");
|
||||||
"SOAC signaling received: type={}, ice_id={:?}",
|
// Guest host: every peer is an assist peer. A logged-in host must keep serving its owner;
|
||||||
soac.msg_type,
|
// assist peers there are already gated by the cloud (allow_control / code check).
|
||||||
soac.ice_id
|
if assist.as_ref().is_some_and(|s| !s.is_account() && !s.allowed) { continue; }
|
||||||
);
|
|
||||||
let ice = soac.ice_id.as_ref().map(|v| v.as_str().map(str::to_owned).unwrap_or_else(|| v.to_string())).unwrap_or_default();
|
let ice = soac.ice_id.as_ref().map(|v| v.as_str().map(str::to_owned).unwrap_or_else(|| v.to_string())).unwrap_or_default();
|
||||||
let mut sessions = webrtc_sessions.lock().await;
|
let mut sessions = webrtc_sessions.lock().await;
|
||||||
// Drop peers that already ended.
|
// Drop peers that already ended.
|
||||||
let ended: Vec<String> = sessions.iter().filter(|(_, s)| matches!(s.pc.connection_state(),
|
let ended: Vec<String> = sessions.iter().filter(|(_, s)| matches!(s.pc.connection_state(),
|
||||||
::webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Closed |
|
::webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Closed |
|
||||||
::webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Failed)).map(|(k, _)| k.clone()).collect();
|
::webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Failed)).map(|(k, _)| k.clone()).collect();
|
||||||
for k in ended { if let Some(old) = sessions.remove(&k) { let _ = old.pc.close().await; } }
|
let mut desktop_ended = false;
|
||||||
|
for k in ended { if let Some(old) = sessions.remove(&k) { desktop_ended |= old.desktop; let _ = old.pc.close().await; } }
|
||||||
|
if desktop_ended && !sessions.values().any(|s| s.desktop) { if let Some(s) = assist.as_mut() { s.session_ended()?; } }
|
||||||
|
if let Some(s) = assist.as_mut() { s.tick()?; }
|
||||||
|
|
||||||
if soac.msg_type == "offer" {
|
if soac.msg_type == "offer" {
|
||||||
let (capture_type,transport) = capture_types.remove(&ice).or_else(|| next_capture_type.take()).unwrap_or((1,crate::session_ice::SessionIce::default()));
|
let (capture_type,transport) = capture_types.remove(&ice).or_else(|| next_capture_type.take()).unwrap_or((1,crate::session_ice::SessionIce::default()));
|
||||||
// CaptureType: 0 unknown, 1 desktop, 2 window; 5 file transfer, 8 terminal, 9 port mapping.
|
// CaptureType: 0 unknown, 1 desktop, 2 window; 5 file transfer, 8 terminal, 9 port mapping.
|
||||||
let desktop = capture_type <= 2;
|
let desktop = capture_type <= 2;
|
||||||
|
let controller_mac = mac_by_ice.remove(&ice).unwrap_or(next_mac);
|
||||||
|
if desktop { input.set_mac_controller(controller_mac); }
|
||||||
log::info!("Offer for connection: capture_type={} desktop={} active_peers={}", capture_type, desktop, sessions.len());
|
log::info!("Offer for connection: capture_type={} desktop={} active_peers={}", capture_type, desktop, sessions.len());
|
||||||
if let Some(old) = sessions.remove(&ice) { let _ = old.pc.close().await; }
|
if let Some(old) = sessions.remove(&ice) { let _ = old.pc.close().await; }
|
||||||
if desktop {
|
if desktop {
|
||||||
@@ -351,6 +656,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
let stale_portal=capture.portal.as_ref().is_some_and(|session|!session.is_alive())
|
let stale_portal=capture.portal.as_ref().is_some_and(|session|!session.is_alive())
|
||||||
&& crate::session_lock::locked_now() == Some(false);
|
&& crate::session_lock::locked_now() == Some(false);
|
||||||
|
if desktop && desktop_backend::headless() {
|
||||||
|
log::info!("Desktop connection ignored: headless mode (terminal, file transfer and port mapping remain available)");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let session_capture=if desktop && (capture.init_error.is_some() || stale_portal) {
|
let session_capture=if desktop && (capture.init_error.is_some() || stale_portal) {
|
||||||
if stale_portal {
|
if stale_portal {
|
||||||
input.release_held();input.bind_portal(None);
|
input.release_held();input.bind_portal(None);
|
||||||
@@ -366,7 +675,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
// current desktop's authorized Portal input session.
|
// current desktop's authorized Portal input session.
|
||||||
if desktop { input.bind_portal(session_capture.portal.clone()); }
|
if desktop { input.bind_portal(session_capture.portal.clone()); }
|
||||||
match WebRtcSession::new(session_capture, input.clone(), signaling_client.clone(), desktop, transport).await {
|
match WebRtcSession::new(session_capture, input.clone(), signaling_client.clone(), desktop, transport).await {
|
||||||
Ok(sess) => match sess.handle_offer(&soac).await {
|
Ok(sess) => {
|
||||||
|
// Register before awaiting SDP: shutdown can always close
|
||||||
|
// a peer whose answer may already be queued to the viewer.
|
||||||
|
sessions.insert(ice.clone(), sess);
|
||||||
|
let offer = sessions.get(&ice).expect("registered peer").handle_offer(&soac).await;
|
||||||
|
match offer {
|
||||||
Ok(answer_sdp) => {
|
Ok(answer_sdp) => {
|
||||||
log::info!("Local SDP Answer generated, emitting soac response...");
|
log::info!("Local SDP Answer generated, emitting soac response...");
|
||||||
let resp = SoacMessage {
|
let resp = SoacMessage {
|
||||||
@@ -381,15 +695,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
app_control_id: soac.app_control_id.clone(),
|
app_control_id: soac.app_control_id.clone(),
|
||||||
};
|
};
|
||||||
if let Err(e) = signaling_client.emit_soac(resp).await { log::error!("Answer signaling send failed: {}", e); }
|
if let Err(e) = signaling_client.emit_soac(resp).await { log::error!("Answer signaling send failed: {}", e); }
|
||||||
sessions.insert(ice, sess);
|
|
||||||
}
|
}
|
||||||
Err(e) => { log::error!("Failed to handle offer: {}", e); let _ = sess.pc.close().await; }
|
Err(_) => {
|
||||||
},
|
log::error!("Failed to handle offer (details omitted)");
|
||||||
Err(e) => log::error!("Create peer failed: {}", e),
|
if let Some(session) = sessions.remove(&ice) { let _ = session.pc.close().await; }
|
||||||
|
}
|
||||||
|
}},
|
||||||
|
Err(_) => log::error!("Create peer failed (details omitted)"),
|
||||||
}
|
}
|
||||||
} else if soac.msg_type == "candidate" {
|
} else if soac.msg_type == "candidate" {
|
||||||
if let Some(sess) = sessions.get(&ice) {
|
if let Some(sess) = sessions.get(&ice) {
|
||||||
if let Err(e) = sess.add_candidate(&soac).await { log::error!("Remote ICE candidate failed: {}", e); }
|
if let Err(e) = sess.add_candidate(&soac).await { let _ = e; log::error!("Remote ICE candidate failed (details omitted)"); }
|
||||||
} else {
|
} else {
|
||||||
log::warn!("Remote ICE candidate for unknown connection");
|
log::warn!("Remote ICE candidate for unknown connection");
|
||||||
}
|
}
|
||||||
@@ -405,11 +721,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
_ => sessions.keys().cloned().collect(),
|
_ => sessions.keys().cloned().collect(),
|
||||||
};
|
};
|
||||||
log::info!("Remote controller disconnected: keys={:?} ice_matched={} closing={}", keys, ice.as_ref().is_some_and(|i| sessions.contains_key(i)), targets.len());
|
log::info!("Remote controller disconnected: keys={:?} ice_matched={} closing={}", keys, ice.as_ref().is_some_and(|i| sessions.contains_key(i)), targets.len());
|
||||||
|
let mut desktop_ended = false;
|
||||||
for k in targets {
|
for k in targets {
|
||||||
if let Some(old) = sessions.remove(&k) {
|
if let Some(old) = sessions.remove(&k) {
|
||||||
|
desktop_ended |= old.desktop;
|
||||||
if let Err(e) = old.pc.close().await { log::warn!("Close released peer failed: {}", e); }
|
if let Err(e) = old.pc.close().await { log::warn!("Close released peer failed: {}", e); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if desktop_ended && !sessions.values().any(|s| s.desktop) { if let Some(s) = assist.as_mut() { s.session_ended()?; } }
|
||||||
|
if let Some(s) = assist.as_mut() { s.tick()?; }
|
||||||
}
|
}
|
||||||
SignalingEvent::Disconnected => {
|
SignalingEvent::Disconnected => {
|
||||||
capture_types.clear();
|
capture_types.clear();
|
||||||
|
|||||||
@@ -1,61 +0,0 @@
|
|||||||
//! Native TCP Port Forwarding & SSH Tunneling via DataChannel
|
|
||||||
|
|
||||||
use std::sync::Arc;
|
|
||||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|
||||||
use tokio::net::TcpStream;
|
|
||||||
use tokio::sync::Mutex;
|
|
||||||
use webrtc::data_channel::data_channel_message::DataChannelMessage;
|
|
||||||
use webrtc::data_channel::RTCDataChannel;
|
|
||||||
|
|
||||||
pub struct PortForwarder;
|
|
||||||
|
|
||||||
impl PortForwarder {
|
|
||||||
/// 将 DataChannel 桥接到本地指定 TCP 端口(默认 127.0.0.1:22 SSH)
|
|
||||||
pub async fn bridge_tcp(
|
|
||||||
target_addr: &str,
|
|
||||||
channel: Arc<RTCDataChannel>,
|
|
||||||
) -> Result<(), String> {
|
|
||||||
let stream = TcpStream::connect(target_addr)
|
|
||||||
.await
|
|
||||||
.map_err(|e| format!("Failed to connect to {}: {}", target_addr, e))?;
|
|
||||||
|
|
||||||
log::info!("Connected to local service {}", target_addr);
|
|
||||||
let (mut tcp_read, mut tcp_write) = stream.into_split();
|
|
||||||
let channel_for_write = channel.clone();
|
|
||||||
|
|
||||||
// 协程 1: 从本地 TCP (如 SSH) 读取数据并写入 DataChannel
|
|
||||||
tokio::spawn(async move {
|
|
||||||
let mut buf = [0u8; 4096];
|
|
||||||
loop {
|
|
||||||
match tcp_read.read(&mut buf).await {
|
|
||||||
Ok(0) => break,
|
|
||||||
Ok(n) => {
|
|
||||||
let bytes = bytes::Bytes::copy_from_slice(&buf[..n]);
|
|
||||||
if let Err(e) = channel_for_write.send(&bytes).await {
|
|
||||||
log::warn!("DataChannel send failed: {}", e);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
log::warn!("TCP read error: {}", e);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// 协程 2: 绑定 DataChannel on_message 写入本地 TCP
|
|
||||||
let tcp_write = Arc::new(Mutex::new(tcp_write));
|
|
||||||
let tcp_write_clone = tcp_write.clone();
|
|
||||||
|
|
||||||
channel.on_message(Box::new(move |msg: DataChannelMessage| {
|
|
||||||
let tcp = tcp_write_clone.clone();
|
|
||||||
Box::pin(async move {
|
|
||||||
let mut lock = tcp.lock().await;
|
|
||||||
let _ = lock.write_all(&msg.data).await;
|
|
||||||
})
|
|
||||||
}));
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,266 @@
|
|||||||
|
//! UU 端口映射 (port mapping) — the controlled ("B") side.
|
||||||
|
//!
|
||||||
|
//! The viewer owns the rules (local port → target_host:target_port reachable from this host).
|
||||||
|
//! For every local TCP connection it opens a stream with Message field 27 `PortMappingFrame
|
||||||
|
//! {1 session_id, 2 rule_id, 3 stream_id, 4 type, 5 payload}` on FILE_DATA_CHANNEL (the official
|
||||||
|
//! host rejects frames from other channels). Frame types: SYN=0, FIN=1, DATA=2, SYN_ACK=3,
|
||||||
|
//! DATA_ACK=4. SYN payload is JSON `{"version", "target_host", "target_port"}`; the host
|
||||||
|
//! connects and answers SYN_ACK `{"version", "ok", "error"}`. DATA carries raw bytes both ways,
|
||||||
|
//! FIN closes a stream, DATA_ACK is ignored by both sides (official log strings). Mirrors
|
||||||
|
//! UURemoteServer `PortMappingHandler` (handleSyn / handleTargetState / flushTargetWrites /
|
||||||
|
//! closeStream). Payload bytes are never logged. `UU_PORT_MAPPING=0` disables it, like the
|
||||||
|
//! official controlled-side switch.
|
||||||
|
|
||||||
|
use crate::protocol::{decode_wire, WireField, WireValue};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
use std::time::Duration;
|
||||||
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
use webrtc::data_channel::RTCDataChannel;
|
||||||
|
|
||||||
|
pub const MESSAGE_FIELD: u32 = 27;
|
||||||
|
const SYN: u64 = 0;
|
||||||
|
const FIN: u64 = 1;
|
||||||
|
const DATA: u64 = 2;
|
||||||
|
const SYN_ACK: u64 = 3;
|
||||||
|
const DATA_ACK: u64 = 4;
|
||||||
|
const PROTOCOL_VERSION: u64 = 1;
|
||||||
|
const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
|
||||||
|
const READ_CHUNK: usize = 16 * 1024;
|
||||||
|
/// Pending writes to one target before the stream is closed ("写缓冲超限").
|
||||||
|
const WRITE_QUEUE_FRAMES: usize = 256;
|
||||||
|
/// Pause reading targets while this much is queued on the data channel.
|
||||||
|
const MAX_BUFFERED: usize = 4 * 1024 * 1024;
|
||||||
|
|
||||||
|
pub fn enabled() -> bool { std::env::var("UU_PORT_MAPPING").map_or(true, |v| v != "0") }
|
||||||
|
|
||||||
|
/// A frame identity field echoed back exactly as received (varint or bytes).
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||||
|
enum Id { Varint(u64), Bytes(Vec<u8>), Missing }
|
||||||
|
|
||||||
|
impl Id {
|
||||||
|
fn of(fields: &[WireField], number: u32) -> Self {
|
||||||
|
match fields.iter().find(|f| f.number == number).map(|f| &f.value) {
|
||||||
|
Some(WireValue::Varint(v)) => Id::Varint(*v),
|
||||||
|
Some(WireValue::Bytes(b)) => Id::Bytes(b.to_vec()),
|
||||||
|
_ => Id::Missing,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn encode(&self, number: u32, out: &mut Vec<u8>) {
|
||||||
|
match self {
|
||||||
|
Id::Varint(v) => { varint(u64::from(number) << 3, out); varint(*v, out); }
|
||||||
|
Id::Bytes(b) => bytes(number, b, out),
|
||||||
|
Id::Missing => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn varint(mut v: u64, out: &mut Vec<u8>) { while v >= 128 { out.push((v as u8 & 127) | 128); v >>= 7; } out.push(v as u8); }
|
||||||
|
fn bytes(number: u32, v: &[u8], out: &mut Vec<u8>) { varint(u64::from(number) << 3 | 2, out); varint(v.len() as u64, out); out.extend_from_slice(v); }
|
||||||
|
|
||||||
|
/// One stream: (session_id, rule_id, stream_id) as sent by the viewer.
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||||
|
struct Key { session: Id, rule: Id, stream: Id }
|
||||||
|
|
||||||
|
fn frame(key: &Key, kind: u64, payload: &[u8]) -> Vec<u8> {
|
||||||
|
let mut out = Vec::new();
|
||||||
|
key.session.encode(1, &mut out);
|
||||||
|
key.rule.encode(2, &mut out);
|
||||||
|
key.stream.encode(3, &mut out);
|
||||||
|
if kind != 0 { varint(4 << 3, &mut out); varint(kind, &mut out); }
|
||||||
|
if !payload.is_empty() { bytes(5, payload, &mut out); }
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
/// SYN payload; accepts the official snake_case keys and camelCase for robustness.
|
||||||
|
fn parse_syn(payload: &[u8]) -> Result<(String, u16), String> {
|
||||||
|
let value: serde_json::Value = serde_json::from_slice(payload).map_err(|_| "invalid SYN payload")?;
|
||||||
|
let host = value.get("target_host").or_else(|| value.get("targetHost")).and_then(|v| v.as_str())
|
||||||
|
.map(str::trim).filter(|h| !h.is_empty()).ok_or("missing target host")?;
|
||||||
|
let port = value.get("target_port").or_else(|| value.get("targetPort")).and_then(|v| v.as_u64())
|
||||||
|
.and_then(|p| u16::try_from(p).ok()).filter(|p| *p != 0).ok_or("invalid target port")?;
|
||||||
|
Ok((host.to_owned(), port))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn syn_ack(ok: bool, error: Option<&str>) -> Vec<u8> {
|
||||||
|
let mut value = serde_json::json!({ "version": PROTOCOL_VERSION, "ok": ok });
|
||||||
|
if let Some(error) = error { value["error"] = serde_json::Value::from(error); }
|
||||||
|
value.to_string().into_bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Session {
|
||||||
|
outbound: mpsc::Sender<(Key, u64, Vec<u8>)>,
|
||||||
|
/// Open streams: writer queue to the target and the task reading from it.
|
||||||
|
streams: Mutex<HashMap<Key, (mpsc::Sender<Vec<u8>>, tokio::task::AbortHandle)>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Session {
|
||||||
|
/// `channel` is the peer's FILE_DATA_CHANNEL slot (filled once the viewer opens it).
|
||||||
|
pub fn new(channel: Arc<Mutex<Option<Arc<RTCDataChannel>>>>, sequence: Arc<AtomicU64>) -> Arc<Self> {
|
||||||
|
let (outbound, mut queue) = mpsc::channel::<(Key, u64, Vec<u8>)>(64);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
while let Some((key, kind, payload)) = queue.recv().await {
|
||||||
|
let mut slot = channel.lock().unwrap().clone();
|
||||||
|
for _ in 0..100 {
|
||||||
|
if slot.is_some() { break; }
|
||||||
|
tokio::time::sleep(Duration::from_millis(50)).await;
|
||||||
|
slot = channel.lock().unwrap().clone();
|
||||||
|
}
|
||||||
|
let Some(dc) = slot else { log::warn!("Port mapping frame dropped: no FILE_DATA_CHANNEL"); continue; };
|
||||||
|
while dc.buffered_amount().await > MAX_BUFFERED {
|
||||||
|
tokio::time::sleep(Duration::from_millis(5)).await;
|
||||||
|
}
|
||||||
|
let timestamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
|
||||||
|
let message = crate::initialization::envelope(sequence.fetch_add(1, Ordering::Relaxed), timestamp,
|
||||||
|
MESSAGE_FIELD, &frame(&key, kind, &payload));
|
||||||
|
if let Err(e) = dc.send(&bytes::Bytes::from(message)).await {
|
||||||
|
log::warn!("Port mapping send failed: {}", e); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Arc::new(Self { outbound, streams: Mutex::new(HashMap::new()) })
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Handles Message field 27 if present; returns true when the message was a port-mapping frame.
|
||||||
|
pub fn handle(self: &Arc<Self>, message: &[WireField], channel: &str) -> bool {
|
||||||
|
let Some(WireValue::Bytes(raw)) = message.iter().find(|f| f.number == MESSAGE_FIELD).map(|f| &f.value) else { return false };
|
||||||
|
if channel != "FILE_DATA_CHANNEL" {
|
||||||
|
log::warn!("Port mapping frame ignored on {}", channel);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
let Ok(fields) = decode_wire(raw) else { log::warn!("Port mapping frame undecodable"); return true };
|
||||||
|
let key = Key { session: Id::of(&fields, 1), rule: Id::of(&fields, 2), stream: Id::of(&fields, 3) };
|
||||||
|
let kind = fields.iter().find_map(|f| match (f.number, &f.value) { (4, WireValue::Varint(v)) => Some(*v), _ => None }).unwrap_or(SYN);
|
||||||
|
let payload = fields.iter().find_map(|f| match (f.number, &f.value) { (5, WireValue::Bytes(b)) => Some(*b), _ => None }).unwrap_or(&[]);
|
||||||
|
match kind {
|
||||||
|
SYN => self.syn(key, payload),
|
||||||
|
DATA => {
|
||||||
|
let writer = self.streams.lock().unwrap().get(&key).map(|s| s.0.clone());
|
||||||
|
match writer {
|
||||||
|
Some(writer) => if writer.try_send(payload.to_vec()).is_err() {
|
||||||
|
log::warn!("Port mapping write buffer full; closing stream");
|
||||||
|
self.close(&key, true);
|
||||||
|
},
|
||||||
|
None => { let _ = self.outbound.try_send((key, FIN, Vec::new())); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FIN => self.close(&key, false),
|
||||||
|
DATA_ACK => {}
|
||||||
|
other => log::info!("Port mapping unsupported frame type {}", other),
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn syn(self: &Arc<Self>, key: Key, payload: &[u8]) {
|
||||||
|
let request = if enabled() { parse_syn(payload) } else { Err("port mapping disabled on this host".into()) };
|
||||||
|
let (host, port) = match request {
|
||||||
|
Ok(target) => target,
|
||||||
|
Err(error) => {
|
||||||
|
log::info!("Port mapping SYN rejected: {}", error);
|
||||||
|
let _ = self.outbound.try_send((key, SYN_ACK, syn_ack(false, Some(&error))));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let session = self.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
log::info!("Port mapping connecting: port={} loopback={}", port, host == "localhost" || host.starts_with("127.") || host == "::1");
|
||||||
|
let connected = tokio::time::timeout(CONNECT_TIMEOUT, tokio::net::TcpStream::connect((host.as_str(), port))).await;
|
||||||
|
let stream = match connected {
|
||||||
|
Ok(Ok(stream)) => stream,
|
||||||
|
Ok(Err(e)) => { session.reply_failure(key, &format!("connect failed: {}", e.kind())).await; return; }
|
||||||
|
Err(_) => { session.reply_failure(key, "connect timeout").await; return; }
|
||||||
|
};
|
||||||
|
let _ = stream.set_nodelay(true);
|
||||||
|
let (mut read, mut write) = stream.into_split();
|
||||||
|
let (writer, mut writes) = mpsc::channel::<Vec<u8>>(WRITE_QUEUE_FRAMES);
|
||||||
|
// SYN_ACK goes out before any DATA from the target (same ordered queue).
|
||||||
|
if session.outbound.send((key.clone(), SYN_ACK, syn_ack(true, None))).await.is_err() { return; }
|
||||||
|
log::info!("Port mapping stream open: port={}", port);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
while let Some(data) = writes.recv().await {
|
||||||
|
if write.write_all(&data).await.is_err() { break; }
|
||||||
|
}
|
||||||
|
let _ = write.shutdown().await;
|
||||||
|
});
|
||||||
|
let reader_session = session.clone();
|
||||||
|
let reader_key = key.clone();
|
||||||
|
let (registered, wait_registered) = tokio::sync::oneshot::channel::<()>();
|
||||||
|
let reader = tokio::spawn(async move {
|
||||||
|
// Start reading only once the stream is registered, so an early close finds it.
|
||||||
|
if wait_registered.await.is_err() { return; }
|
||||||
|
let mut buf = vec![0u8; READ_CHUNK];
|
||||||
|
loop {
|
||||||
|
match read.read(&mut buf).await {
|
||||||
|
Ok(0) | Err(_) => break,
|
||||||
|
Ok(n) => if reader_session.outbound.send((reader_key.clone(), DATA, buf[..n].to_vec())).await.is_err() { return; },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
reader_session.close(&reader_key, true);
|
||||||
|
});
|
||||||
|
session.streams.lock().unwrap().insert(key, (writer, reader.abort_handle()));
|
||||||
|
let _ = registered.send(());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn reply_failure(&self, key: Key, error: &str) {
|
||||||
|
log::info!("Port mapping connect failed: {}", error);
|
||||||
|
let _ = self.outbound.send((key, SYN_ACK, syn_ack(false, Some(error)))).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Drops the stream: the writer task shuts the target down and the reader is aborted;
|
||||||
|
/// optionally tells the viewer.
|
||||||
|
fn close(&self, key: &Key, send_fin: bool) {
|
||||||
|
let removed = self.streams.lock().unwrap().remove(key);
|
||||||
|
if let Some((_, reader)) = removed {
|
||||||
|
reader.abort();
|
||||||
|
if send_fin { let _ = self.outbound.try_send((key.clone(), FIN, Vec::new())); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The peer connection ended: close every target connection.
|
||||||
|
pub fn shutdown(&self) {
|
||||||
|
let streams: Vec<_> = self.streams.lock().unwrap().drain().collect();
|
||||||
|
if !streams.is_empty() { log::info!("Port mapping closed {} stream(s) with the connection", streams.len()); }
|
||||||
|
for (_, (_, reader)) in streams { reader.abort(); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn syn_payload_uses_official_snake_case_keys() {
|
||||||
|
assert_eq!(parse_syn(br#"{"version":1,"target_host":"127.0.0.1","target_port":22}"#).unwrap(), ("127.0.0.1".into(), 22));
|
||||||
|
assert_eq!(parse_syn(br#"{"targetHost":"nas.local","targetPort":8080}"#).unwrap(), ("nas.local".into(), 8080));
|
||||||
|
assert!(parse_syn(br#"{"target_host":"","target_port":22}"#).is_err());
|
||||||
|
assert!(parse_syn(br#"{"target_host":"h","target_port":70000}"#).is_err());
|
||||||
|
assert!(parse_syn(b"not json").is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn syn_ack_reports_ok_and_error() {
|
||||||
|
let ok: serde_json::Value = serde_json::from_slice(&syn_ack(true, None)).unwrap();
|
||||||
|
assert_eq!(ok, serde_json::json!({"version": 1, "ok": true}));
|
||||||
|
let failed: serde_json::Value = serde_json::from_slice(&syn_ack(false, Some("connect timeout"))).unwrap();
|
||||||
|
assert_eq!(failed["ok"], false);
|
||||||
|
assert_eq!(failed["error"], "connect timeout");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn frames_echo_identity_fields_with_their_wire_types() {
|
||||||
|
let key = Key { session: Id::Bytes(b"s1".to_vec()), rule: Id::Varint(7), stream: Id::Varint(300) };
|
||||||
|
let encoded = frame(&key, DATA, b"abc");
|
||||||
|
let fields = decode_wire(&encoded).unwrap();
|
||||||
|
assert_eq!(Id::of(&fields, 1), Id::Bytes(b"s1".to_vec()));
|
||||||
|
assert_eq!(Id::of(&fields, 2), Id::Varint(7));
|
||||||
|
assert_eq!(Id::of(&fields, 3), Id::Varint(300));
|
||||||
|
assert!(fields.iter().any(|f| f.number == 4 && f.value == WireValue::Varint(DATA)));
|
||||||
|
assert!(fields.iter().any(|f| f.number == 5 && f.value == WireValue::Bytes(b"abc")));
|
||||||
|
// SYN is the proto3 default (0) and is omitted on the wire.
|
||||||
|
let syn = frame(&key, SYN, b"");
|
||||||
|
assert!(!decode_wire(&syn).unwrap().iter().any(|f| f.number == 4 || f.number == 5));
|
||||||
|
}
|
||||||
|
}
|
||||||
+15
-4
@@ -50,13 +50,24 @@ 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 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 handle = NonNull::new(pointer).ok_or_else(|| super::error(error))?;
|
||||||
Ok(Self { handle: Mutex::new(Handle(handle)), locked: crate::session_lock::watch_locked(), was_locked: std::sync::atomic::AtomicBool::new(true), retry_at: Mutex::new(None), width, 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 })
|
||||||
}
|
}
|
||||||
pub fn capture(&self, frame: &mut [u8]) -> Result<(), String> {
|
pub fn capture(&self, frame: &mut [u8]) -> Result<(), String> {
|
||||||
let mut handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?;
|
let mut handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?;
|
||||||
let locked = *self.locked.borrow() != Some(false);
|
let state = *self.locked.borrow();
|
||||||
if locked || self.was_locked.swap(locked, std::sync::atomic::Ordering::AcqRel) {
|
if state != Some(false) {
|
||||||
self.was_locked.store(locked, std::sync::atomic::Ordering::Release);
|
// Unknown state (watcher not reported yet): show nothing but keep the cached frame.
|
||||||
|
if state == Some(true) {
|
||||||
|
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
|
||||||
|
unsafe { uu_portal_reset_frame(handle.0.as_ptr()) };
|
||||||
|
}
|
||||||
|
super::blank(frame); return Ok(());
|
||||||
|
}
|
||||||
|
if self.was_locked.swap(false, std::sync::atomic::Ordering::AcqRel) {
|
||||||
|
// First unlocked capture after a real lock: drop frames captured while locked.
|
||||||
unsafe { uu_portal_reset_frame(handle.0.as_ptr()) };
|
unsafe { uu_portal_reset_frame(handle.0.as_ptr()) };
|
||||||
super::blank(frame); return Ok(());
|
super::blank(frame); return Ok(());
|
||||||
}
|
}
|
||||||
|
|||||||
+137
-13
@@ -32,13 +32,16 @@ pub struct SoacMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub enum SignalingEvent {
|
pub enum SignalingEvent {
|
||||||
|
Stop,
|
||||||
|
AssistCommand(crate::assist::Command),
|
||||||
|
AssistTick,
|
||||||
Connected,
|
Connected,
|
||||||
RoomInfoAck(Value),
|
RoomInfoAck(Value),
|
||||||
DeviceInfoChanged(Value),
|
DeviceInfoChanged(Value),
|
||||||
BeControlled(Value),
|
BeControlled(Value),
|
||||||
/// be-controlled with its binary app_data (ConnectOptions): capture type and the
|
/// be-controlled with its binary app_data (ConnectOptions): capture type and the
|
||||||
/// ice_id it announces, when present.
|
/// ice_id it announces, when present.
|
||||||
ConnectRequest { capture_type: u64, ice_id: Option<String>, transport:crate::session_ice::SessionIce },
|
ConnectRequest { capture_type: u64, ice_id: Option<String>, transport:crate::session_ice::SessionIce, controller_mac: bool },
|
||||||
Soac(SoacMessage),
|
Soac(SoacMessage),
|
||||||
Released(Value),
|
Released(Value),
|
||||||
Left(Value),
|
Left(Value),
|
||||||
@@ -47,6 +50,51 @@ pub enum SignalingEvent {
|
|||||||
|
|
||||||
pub struct SignalingClient {
|
pub struct SignalingClient {
|
||||||
tx_outgoing: mpsc::Sender<String>,
|
tx_outgoing: mpsc::Sender<String>,
|
||||||
|
// Belongs to this room only; fresh-room reconnect must not reuse it.
|
||||||
|
_reconnect_key: Arc<std::sync::Mutex<Option<String>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn refreshed_key(ack:&Value)->Option<String> {
|
||||||
|
let values=ack.as_array()?;
|
||||||
|
if values.first()?.as_str()? != "success" { return None; }
|
||||||
|
let key=values.get(1)?.get("reconnect_key")?.as_str()?;
|
||||||
|
(!key.is_empty() && key.len()<=16384).then(||key.to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod refresh_tests {
|
||||||
|
use super::refreshed_key;
|
||||||
|
use serde_json::json;
|
||||||
|
#[test]
|
||||||
|
fn accepts_only_successful_complete_reconnect_key_ack() {
|
||||||
|
assert_eq!(refreshed_key(&json!(["success",{"reconnect_key":"synthetic-key"}])),Some("synthetic-key".into()));
|
||||||
|
for ack in [json!(["failure",{"reconnect_key":"synthetic-key"}]),json!(["success"]),json!(["success",{"reconnect_key":""}]),json!(["success",{"reconnect_key":123}]),json!({"reconnect_key":"synthetic-key"})] {
|
||||||
|
assert!(refreshed_key(&ack).is_none());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn closes_obsolete_socket_even_if_peer_retains_client() {
|
||||||
|
use futures_util::{SinkExt,StreamExt};
|
||||||
|
use tokio_tungstenite::tungstenite::Message;
|
||||||
|
let listener=tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||||
|
let address=listener.local_addr().unwrap();
|
||||||
|
let server=tokio::spawn(async move {
|
||||||
|
let (tcp,_)=listener.accept().await.unwrap();
|
||||||
|
let mut socket=tokio_tungstenite::accept_async(tcp).await.unwrap();
|
||||||
|
socket.send(Message::Text("0{}".into())).await.unwrap();
|
||||||
|
assert_eq!(socket.next().await.unwrap().unwrap(),Message::Text("40".into()));
|
||||||
|
socket.send(Message::Text("40{}".into())).await.unwrap();
|
||||||
|
assert!(socket.next().await.unwrap().unwrap().into_text().unwrap().contains("room_info"));
|
||||||
|
matches!(socket.next().await,None|Some(Err(_))|Some(Ok(Message::Close(_))))
|
||||||
|
});
|
||||||
|
let (sender,mut events)=tokio::sync::mpsc::channel(4);
|
||||||
|
let retained=super::SignalingClient::connect(&format!("ws://{address}"),"synthetic-only",sender).await.unwrap();
|
||||||
|
assert!(matches!(events.recv().await,Some(super::SignalingEvent::Connected)));
|
||||||
|
drop(events);
|
||||||
|
assert!(tokio::time::timeout(std::time::Duration::from_secs(2),server).await.unwrap().unwrap());
|
||||||
|
// Retain it until after the server sees closure, just like PC callbacks.
|
||||||
|
drop(retained);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SignalingClient {
|
impl SignalingClient {
|
||||||
@@ -74,38 +122,82 @@ impl SignalingClient {
|
|||||||
.await
|
.await
|
||||||
.map_err(|e| format!("WS connect failed: {}", e))?;
|
.map_err(|e| format!("WS connect failed: {}", e))?;
|
||||||
|
|
||||||
log::info!("Connected to Engine.IO / Socket.IO at {}", ws_url);
|
log::info!("Connected to Engine.IO / Socket.IO");
|
||||||
let (mut write, mut read) = ws_stream.split();
|
let (mut write, mut read) = ws_stream.split();
|
||||||
let (tx_outgoing, mut rx_outgoing) = mpsc::channel::<String>(32);
|
let (tx_outgoing, mut rx_outgoing) = mpsc::channel::<String>(32);
|
||||||
|
|
||||||
// 发送协程
|
// 发送协程
|
||||||
tokio::spawn(async move {
|
let writer=tokio::spawn(async move {
|
||||||
while let Some(msg) = rx_outgoing.recv().await {
|
while let Some(msg) = rx_outgoing.recv().await {
|
||||||
if let Err(e) = write.send(Message::Text(msg)).await {
|
if let Err(e) = write.send(Message::Text(msg)).await {
|
||||||
log::warn!("WS send error: {}", e);
|
let _ = e;
|
||||||
|
log::warn!("WS send failed (details omitted)");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let tx_out_clone = tx_outgoing.clone();
|
let tx_out_clone = tx_outgoing.clone();
|
||||||
|
static GENERATION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
|
||||||
|
let generation=GENERATION.fetch_add(1,std::sync::atomic::Ordering::Relaxed);
|
||||||
|
let reconnect_key=Arc::new(std::sync::Mutex::new(None));
|
||||||
|
let refreshed=reconnect_key.clone();
|
||||||
|
|
||||||
// 接收与状态机协程
|
// 接收与状态机协程
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let mut req_counter = 10;
|
let mut req_counter = 10;
|
||||||
|
let started=std::time::Instant::now();
|
||||||
|
let mut last_ping=started;
|
||||||
|
let mut pings=0u64;
|
||||||
|
let mut pong_queued=0u64;
|
||||||
|
let mut connected=false;
|
||||||
|
let mut next_refresh=tokio::time::Instant::now()+std::time::Duration::from_secs(30);
|
||||||
|
let mut refresh_acks=std::collections::VecDeque::new();
|
||||||
|
let mut accepted_refresh=0;
|
||||||
// Socket.IO binary event awaiting attachments: (event name, header JSON).
|
// Socket.IO binary event awaiting attachments: (event name, header JSON).
|
||||||
let mut pending_binary: Option<(String, Value)> = None;
|
let mut pending_binary: Option<(String, Value)> = None;
|
||||||
while let Some(msg_res) = read.next().await {
|
loop {
|
||||||
|
let msg_res=tokio::select! {
|
||||||
|
_=event_sender.closed()=>break,
|
||||||
|
// Official ControlledSession RefreshSignalTokenLoop: first
|
||||||
|
// 30s, success every ~300s, failure retry after 30s. Native
|
||||||
|
// libstreamer 0x885774 / 0x885e58; emit has NO payload args.
|
||||||
|
_=tokio::time::sleep_until(next_refresh), if connected => {
|
||||||
|
req_counter+=1;
|
||||||
|
refresh_acks.push_back(req_counter);
|
||||||
|
if refresh_acks.len()>16 { refresh_acks.pop_front(); }
|
||||||
|
let packet=format!("42{}[\"refresh_reconnect_key\"]",req_counter);
|
||||||
|
if tx_out_clone.send(packet).await.is_err() {
|
||||||
|
let _=event_sender.send(SignalingEvent::Disconnected).await;break;
|
||||||
|
}
|
||||||
|
next_refresh=tokio::time::Instant::now()+std::time::Duration::from_secs(30);
|
||||||
|
log::info!("Signaling reconnect-key refresh requested: generation={} age_s={}",generation,started.elapsed().as_secs());
|
||||||
|
continue;
|
||||||
|
},
|
||||||
|
msg=read.next()=>match msg {
|
||||||
|
Some(msg)=>msg,
|
||||||
|
None=>{let _=event_sender.send(SignalingEvent::Disconnected).await;break;}
|
||||||
|
},
|
||||||
|
};
|
||||||
match msg_res {
|
match msg_res {
|
||||||
Ok(Message::Text(txt)) => {
|
Ok(Message::Text(txt)) => {
|
||||||
// Engine.IO 报文处理
|
// Engine.IO 报文处理
|
||||||
if txt.starts_with('0') {
|
if txt.starts_with('0') {
|
||||||
|
if let Ok(open)=serde_json::from_str::<Value>(&txt[1..]) {
|
||||||
|
log::info!("Signaling heartbeat config: generation={} ping_interval_ms={:?} ping_timeout_ms={:?}",generation,open.get("pingInterval").and_then(Value::as_u64),open.get("pingTimeout").and_then(Value::as_u64));
|
||||||
|
}
|
||||||
// Open packet -> 发送 Socket.IO connect
|
// Open packet -> 发送 Socket.IO connect
|
||||||
let _ = tx_out_clone.send("40".into()).await;
|
let _ = tx_out_clone.send("40".into()).await;
|
||||||
} else if txt == "2" {
|
} else if txt == "2" {
|
||||||
// Ping -> 回复 Pong
|
// Ping -> 回复 Pong
|
||||||
let _ = tx_out_clone.send("3".into()).await;
|
let gap=last_ping.elapsed().as_secs();last_ping=std::time::Instant::now();pings+=1;
|
||||||
|
if tx_out_clone.send("3".into()).await.is_ok() { pong_queued+=1; }
|
||||||
|
if pings==1 || pings%12==0 {
|
||||||
|
log::info!("Signaling heartbeat progress: generation={} age_s={} pings={} pong_queued={} gap_s={}",generation,started.elapsed().as_secs(),pings,pong_queued,gap);
|
||||||
|
}
|
||||||
} else if txt.starts_with("40") {
|
} else if txt.starts_with("40") {
|
||||||
|
connected=true;
|
||||||
|
next_refresh=tokio::time::Instant::now()+std::time::Duration::from_secs(30);
|
||||||
// Socket.IO connected -> 发送 room_info 请求
|
// Socket.IO connected -> 发送 room_info 请求
|
||||||
req_counter += 1;
|
req_counter += 1;
|
||||||
let room_req = format!("42{}[\"room_info\"]", req_counter);
|
let room_req = format!("42{}[\"room_info\"]", req_counter);
|
||||||
@@ -113,9 +205,23 @@ impl SignalingClient {
|
|||||||
let _ = event_sender.send(SignalingEvent::Connected).await;
|
let _ = event_sender.send(SignalingEvent::Connected).await;
|
||||||
} else if txt.starts_with("43") {
|
} else if txt.starts_with("43") {
|
||||||
// Socket.IO ack 回调
|
// Socket.IO ack 回调
|
||||||
let payload = &txt[txt.find('[').unwrap_or(2)..];
|
let begin=txt.find('[').unwrap_or(2);
|
||||||
|
let ack_id=txt[2..begin].parse::<u64>().ok();
|
||||||
|
let payload = &txt[begin..];
|
||||||
if let Ok(v) = serde_json::from_str::<Value>(payload) {
|
if let Ok(v) = serde_json::from_str::<Value>(payload) {
|
||||||
let _ = event_sender.send(SignalingEvent::RoomInfoAck(v)).await;
|
if let Some(id)=ack_id.filter(|id|refresh_acks.contains(id)) {
|
||||||
|
refresh_acks.retain(|pending|*pending!=id);
|
||||||
|
let key=refreshed_key(&v);
|
||||||
|
let success=key.is_some();
|
||||||
|
// Accept a delayed retry response, but never replace
|
||||||
|
// a newer key with an older successful response.
|
||||||
|
if success && id>accepted_refresh {
|
||||||
|
accepted_refresh=id;
|
||||||
|
*refreshed.lock().unwrap()=key;
|
||||||
|
next_refresh=tokio::time::Instant::now()+std::time::Duration::from_secs(300);
|
||||||
|
}
|
||||||
|
log::info!("Signaling reconnect-key refresh acknowledged: generation={} age_s={} success={}",generation,started.elapsed().as_secs(),success);
|
||||||
|
} else { let _ = event_sender.send(SignalingEvent::RoomInfoAck(v)).await; }
|
||||||
}
|
}
|
||||||
} else if txt.starts_with("45") || txt.starts_with("46") {
|
} else if txt.starts_with("45") || txt.starts_with("46") {
|
||||||
// Socket.IO binary event/ack: header text, attachments follow as binary frames.
|
// Socket.IO binary event/ack: header text, attachments follow as binary frames.
|
||||||
@@ -165,19 +271,22 @@ impl SignalingClient {
|
|||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Failed to deserialize SoacMessage: error={}, raw={}", e, data);
|
let _ = e;
|
||||||
|
log::error!("Failed to deserialize SoacMessage (payload omitted)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"released" => {
|
"released" => {
|
||||||
|
log::info!("Cloud peer event: generation={} event=released age_s={} pings={} last_ping_gap_s={} ice_id_present={}",generation,started.elapsed().as_secs(),pings,last_ping.elapsed().as_secs(),data.get("ice_id").is_some());
|
||||||
let _ = event_sender
|
let _ = event_sender
|
||||||
.send(SignalingEvent::Released(data.clone()))
|
.send(SignalingEvent::Released(data.clone()))
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
"left" | "leave" => {
|
"left" | "leave" => {
|
||||||
|
log::info!("Cloud peer event: generation={} event={} age_s={} pings={} last_ping_gap_s={} ice_id_present={}",generation,event_name,started.elapsed().as_secs(),pings,last_ping.elapsed().as_secs(),data.get("ice_id").is_some());
|
||||||
let ice_id = data.get("ice_id").and_then(|v| v.as_str()).unwrap_or("");
|
let ice_id = data.get("ice_id").and_then(|v| v.as_str()).unwrap_or("");
|
||||||
if ice_id.is_empty() {
|
if ice_id.is_empty() {
|
||||||
log::warn!("Room expired from NetEase cloud (leave event). Triggering room renewal...");
|
log::warn!("Cloud room leave without peer identifier; renewing signaling (expiry cause unknown)");
|
||||||
let _ = event_sender.send(SignalingEvent::Disconnected).await;
|
let _ = event_sender.send(SignalingEvent::Disconnected).await;
|
||||||
} else {
|
} else {
|
||||||
let _ = event_sender
|
let _ = event_sender
|
||||||
@@ -208,20 +317,35 @@ impl SignalingClient {
|
|||||||
let ice_id = ["ice_id", "iceId"].iter().find_map(|k| header.get(*k).or_else(|| header.get("data").and_then(|d| d.get(*k))))
|
let ice_id = ["ice_id", "iceId"].iter().find_map(|k| header.get(*k).or_else(|| header.get("data").and_then(|d| d.get(*k))))
|
||||||
.and_then(|v| v.as_str()).map(str::to_owned);
|
.and_then(|v| v.as_str()).map(str::to_owned);
|
||||||
log::info!("be-controlled connect request: capture_type={} header_keys={:?} ice_id_present={}", capture_type, keys, ice_id.is_some());
|
log::info!("be-controlled connect request: capture_type={} header_keys={:?} ice_id_present={}", capture_type, keys, ice_id.is_some());
|
||||||
let _ = event_sender.send(SignalingEvent::ConnectRequest { capture_type, ice_id, transport:crate::session_ice::SessionIce::from_header(&header) }).await;
|
// Viewer platform (website enum: 1 windows, 2 android, 3 ios, 4 macos, 5 tv).
|
||||||
|
let platform = header.get("controller_platform").or_else(|| header.get("data").and_then(|d| d.get("controller_platform")));
|
||||||
|
let controller_mac = platform.is_some_and(|v| v.as_i64() == Some(4)
|
||||||
|
|| v.as_str().is_some_and(|s| s == "4" || s.to_ascii_lowercase().contains("mac") || s.to_ascii_lowercase().contains("darwin")));
|
||||||
|
log::info!("Controller platform: {} mac={}", platform.map(|v| v.to_string()).unwrap_or_default().chars().take(24).collect::<String>(), controller_mac);
|
||||||
|
let _ = event_sender.send(SignalingEvent::ConnectRequest { capture_type, ice_id, transport:crate::session_ice::SessionIce::from_header(&header), controller_mac }).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Message::Close(_)) | Err(_) => {
|
Ok(Message::Close(frame)) => {
|
||||||
|
log::warn!("Signaling socket closed: generation={} age_s={} close_code={:?} pings={} last_ping_gap_s={}",generation,started.elapsed().as_secs(),frame.map(|f|u16::from(f.code)),pings,last_ping.elapsed().as_secs());
|
||||||
|
let _ = event_sender.send(SignalingEvent::Disconnected).await;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
log::warn!("Signaling socket receive failed: generation={} age_s={} pings={} last_ping_gap_s={}",generation,started.elapsed().as_secs(),pings,last_ping.elapsed().as_secs());
|
||||||
let _ = event_sender.send(SignalingEvent::Disconnected).await;
|
let _ = event_sender.send(SignalingEvent::Disconnected).await;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log::info!("Signaling receiver stopped: generation={} age_s={}",generation,started.elapsed().as_secs());
|
||||||
|
// Live peer callbacks can retain the old SignalingClient sender.
|
||||||
|
// Explicitly drop its write half too when this room stops reading.
|
||||||
|
writer.abort();
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(Self { tx_outgoing })
|
Ok(Self { tx_outgoing, _reconnect_key:reconnect_key })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn emit_event(&self, event: &str, payload: serde_json::Value) -> Result<(), String> {
|
pub async fn emit_event(&self, event: &str, payload: serde_json::Value) -> Result<(), String> {
|
||||||
|
|||||||
+10
-5
@@ -113,10 +113,14 @@ pub fn args_shape(args: &[u8]) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn mute_for_session(state: &mut HostState) {
|
fn mute_for_session(state: &mut HostState) {
|
||||||
if state.restore.is_none() { state.restore = host_muted(); }
|
if state.restore.is_none() {
|
||||||
set_host_muted(true);
|
state.restore = host_muted();
|
||||||
|
set_host_muted(true);
|
||||||
|
}
|
||||||
|
crate::virtual_audio::enable();
|
||||||
}
|
}
|
||||||
fn restore_mute(state: &mut HostState) {
|
fn restore_mute(state: &mut HostState) {
|
||||||
|
crate::virtual_audio::disable();
|
||||||
if let Some(previous) = state.restore.take() { set_host_muted(previous); }
|
if let Some(previous) = state.restore.take() { set_host_muted(previous); }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,20 +147,20 @@ impl PeerActions {
|
|||||||
let on = toggle_value(value, args);
|
let on = toggle_value(value, args);
|
||||||
state.settings.keep_mute = on;
|
state.settings.keep_mute = on;
|
||||||
if on { mute_for_session(&mut state); } else { restore_mute(&mut state); }
|
if on { mute_for_session(&mut state); } else { restore_mute(&mut state); }
|
||||||
log::info!("System action mute (action {}): on={} muted={:?}", action, on, host_muted());
|
log::info!("System action mute (action {}): on={} virtual_sink={} default_muted={:?}", action, on, crate::virtual_audio::active(), host_muted());
|
||||||
}
|
}
|
||||||
// Keep the host muted after the session (远程结束后,恢复被控端声音设置 unchecked).
|
// Keep the host muted after the session (远程结束后,恢复被控端声音设置 unchecked).
|
||||||
MUTE_PERMANENT => {
|
MUTE_PERMANENT => {
|
||||||
let on = toggle_value(value, args);
|
let on = toggle_value(value, args);
|
||||||
state.settings.mute_permanent = on;
|
state.settings.mute_permanent = on;
|
||||||
if on { state.settings.keep_mute = true; mute_for_session(&mut state); }
|
if on { state.settings.keep_mute = true; mute_for_session(&mut state); }
|
||||||
log::info!("System action mute_permanent: on={} muted={:?}", on, host_muted());
|
log::info!("System action mute_permanent: on={} virtual_sink={} default_muted={:?}", on, crate::virtual_audio::active(), host_muted());
|
||||||
}
|
}
|
||||||
MUTE_RESTORE => {
|
MUTE_RESTORE => {
|
||||||
state.settings.keep_mute = false;
|
state.settings.keep_mute = false;
|
||||||
state.settings.mute_permanent = false;
|
state.settings.mute_permanent = false;
|
||||||
restore_mute(&mut state);
|
restore_mute(&mut state);
|
||||||
log::info!("System action mute_restore: muted={:?}", host_muted());
|
log::info!("System action mute_restore: virtual_sink={} default_muted={:?}", crate::virtual_audio::active(), host_muted());
|
||||||
}
|
}
|
||||||
SET_AUTO_LOCK => {
|
SET_AUTO_LOCK => {
|
||||||
// Observed: enabling 远程结束后自动锁屏 sends value 2 and disabling value 1.
|
// Observed: enabling 远程结束后自动锁屏 sends value 2 and disabling value 1.
|
||||||
@@ -188,6 +192,7 @@ impl PeerActions {
|
|||||||
let mut state = state().lock().unwrap();
|
let mut state = state().lock().unwrap();
|
||||||
state.desktop_sessions = state.desktop_sessions.saturating_sub(1);
|
state.desktop_sessions = state.desktop_sessions.saturating_sub(1);
|
||||||
if state.desktop_sessions > 0 { return; }
|
if state.desktop_sessions > 0 { return; }
|
||||||
|
crate::virtual_audio::disable();
|
||||||
if !state.settings.mute_permanent { restore_mute(&mut state); }
|
if !state.settings.mute_permanent { restore_mute(&mut state); }
|
||||||
if state.settings.auto_lock {
|
if state.settings.auto_lock {
|
||||||
log::info!("Auto lock after remote session: ok={}", lock_session());
|
log::info!("Auto lock after remote session: ok={}", lock_session());
|
||||||
|
|||||||
+56
-21
@@ -104,7 +104,7 @@ static NEXT_ID: AtomicU32 = AtomicU32::new(1);
|
|||||||
|
|
||||||
fn now_ms() -> u64 { std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_millis() as u64 }
|
fn now_ms() -> u64 { std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_millis() as u64 }
|
||||||
|
|
||||||
fn spawn(cols: u16, rows: u16, shell: Option<&str>) -> Result<Arc<Session>, String> {
|
fn spawn(requested_id: u32, cols: u16, rows: u16, shell: Option<&str>) -> Result<Arc<Session>, String> {
|
||||||
use std::os::unix::process::CommandExt;
|
use std::os::unix::process::CommandExt;
|
||||||
let win = nix::pty::Winsize { ws_row: rows.max(1), ws_col: cols.max(1), ws_xpixel: 0, ws_ypixel: 0 };
|
let win = nix::pty::Winsize { ws_row: rows.max(1), ws_col: cols.max(1), ws_xpixel: 0, ws_ypixel: 0 };
|
||||||
let pty = nix::pty::openpty(Some(&win), None).map_err(|e| format!("openpty failed: {e}"))?;
|
let pty = nix::pty::openpty(Some(&win), None).map_err(|e| format!("openpty failed: {e}"))?;
|
||||||
@@ -128,7 +128,13 @@ fn spawn(cols: u16, rows: u16, shell: Option<&str>) -> Result<Arc<Session>, Stri
|
|||||||
}
|
}
|
||||||
let mut child = cmd.spawn().map_err(|e| format!("shell spawn failed: {e}"))?;
|
let mut child = cmd.spawn().map_err(|e| format!("shell spawn failed: {e}"))?;
|
||||||
drop(slave);
|
drop(slave);
|
||||||
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
|
// Official Mac 4.42.0 (TerminalManager.open) allocates an ID only for OPEN(0).
|
||||||
|
let id = if requested_id != 0 { requested_id } else {
|
||||||
|
loop {
|
||||||
|
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
|
||||||
|
if id != 0 && !registry().lock().unwrap().contains_key(&id) { break id; }
|
||||||
|
}
|
||||||
|
};
|
||||||
let session = Arc::new(Session { id, shell: shell.clone(), created_ms: now_ms(), pid: child.id() as i32,
|
let session = Arc::new(Session { id, shell: shell.clone(), created_ms: now_ms(), pid: child.id() as i32,
|
||||||
master: pty.master, sink: Mutex::new(None), scrollback: Mutex::new(VecDeque::new()) });
|
master: pty.master, sink: Mutex::new(None), scrollback: Mutex::new(VecDeque::new()) });
|
||||||
let mut reader = std::fs::File::from(session.master.try_clone().map_err(|e| format!("pty dup failed: {e}"))?);
|
let mut reader = std::fs::File::from(session.master.try_clone().map_err(|e| format!("pty dup failed: {e}"))?);
|
||||||
@@ -207,12 +213,14 @@ impl TerminalConn {
|
|||||||
let (cols, rows) = dims(&v);
|
let (cols, rows) = dims(&v);
|
||||||
let session = match existing {
|
let session = match existing {
|
||||||
Some(s) => { s.resize(cols, rows); Ok(s) }
|
Some(s) => { s.resize(cols, rows); Ok(s) }
|
||||||
None => spawn(cols, rows, v.get("shell").and_then(|s| s.as_str())),
|
None => spawn(frame.session, cols, rows, v.get("shell").and_then(|s| s.as_str())),
|
||||||
};
|
};
|
||||||
match session {
|
match session {
|
||||||
Ok(s) => {
|
Ok(s) => {
|
||||||
*s.sink.lock().unwrap() = Some(self.out.clone());
|
// Queue the acknowledgement before the PTY reader can emit live output.
|
||||||
|
let mut sink = s.sink.lock().unwrap();
|
||||||
self.send(OPEN_RESP, s.id, &reply(0, "ok"));
|
self.send(OPEN_RESP, s.id, &reply(0, "ok"));
|
||||||
|
*sink = Some(self.out.clone());
|
||||||
let replay: Vec<u8> = s.scrollback.lock().unwrap().iter().copied().collect();
|
let replay: Vec<u8> = s.scrollback.lock().unwrap().iter().copied().collect();
|
||||||
if !replay.is_empty() { self.send(STDOUT, s.id, &replay); }
|
if !replay.is_empty() { self.send(STDOUT, s.id, &replay); }
|
||||||
}
|
}
|
||||||
@@ -241,7 +249,8 @@ impl TerminalConn {
|
|||||||
"state": if attached { "running" } else { "detached" }, "created_at_ms": s.created_ms})
|
"state": if attached { "running" } else { "detached" }, "created_at_ms": s.created_ms})
|
||||||
}).collect();
|
}).collect();
|
||||||
log::info!("Terminal list sessions: count={}", sessions.len());
|
log::info!("Terminal list sessions: count={}", sessions.len());
|
||||||
self.send(LIST_RESP, 0, serde_json::json!({"code": 0, "message": "ok", "sessions": sessions}).to_string().as_bytes());
|
// Official Mac 4.42.0 (handleListSessionsReq) sends a bare array with the request's header ID.
|
||||||
|
self.send(LIST_RESP, frame.session, serde_json::Value::Array(sessions).to_string().as_bytes());
|
||||||
}
|
}
|
||||||
UNLOCK_REQ => self.send(UNLOCK_RESP, frame.session, &reply(0, "ok")),
|
UNLOCK_REQ => self.send(UNLOCK_RESP, frame.session, &reply(0, "ok")),
|
||||||
_ => self.send(ERROR, frame.session, &reply(1, "unsupported frame")),
|
_ => self.send(ERROR, frame.session, &reply(1, "unsupported frame")),
|
||||||
@@ -268,25 +277,51 @@ mod tests {
|
|||||||
assert_eq!(signal_number(b"whatever"), libc::SIGINT);
|
assert_eq!(signal_number(b"whatever"), libc::SIGINT);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn open_spawns_shell_and_streams_output() {
|
fn list_reply_is_an_array_and_preserves_request_id() {
|
||||||
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
|
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
|
||||||
let conn = TerminalConn::new(tx);
|
let conn = TerminalConn::new(tx);
|
||||||
conn.handle(&encode(OPEN, 0, br#"{"cols":100,"rows":30,"shell":"/bin/sh"}"#));
|
for request in [0, 7, u32::MAX] {
|
||||||
let resp = decode(&rx.blocking_recv().unwrap()).unwrap();
|
conn.handle(&encode(LIST_REQ, request, &[]));
|
||||||
assert_eq!(resp.kind, OPEN_RESP);
|
let response = decode(&rx.try_recv().unwrap()).unwrap();
|
||||||
assert_eq!(json(&resp.payload)["code"], 0);
|
assert_eq!(response.kind, LIST_RESP);
|
||||||
conn.handle(&encode(STDIN, resp.session, b"echo uu-term-ok; exit 3\n"));
|
assert_eq!(response.session, request);
|
||||||
let mut seen = Vec::new();
|
assert!(json(&response.payload).is_array());
|
||||||
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
|
}
|
||||||
let mut exit = None;
|
}
|
||||||
while std::time::Instant::now() < deadline && exit.is_none() {
|
#[test]
|
||||||
if let Ok(m) = rx.try_recv() {
|
fn open_spawns_shell_and_streams_output() {
|
||||||
let f = decode(&m).unwrap();
|
for requested_id in [0, u32::MAX] {
|
||||||
if f.kind == STDOUT { seen.extend(f.payload) } else if f.kind == EXIT { exit = Some(json(&f.payload)["exit_code"].clone()) }
|
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
|
||||||
} else { std::thread::sleep(std::time::Duration::from_millis(20)); }
|
let conn = TerminalConn::new(tx);
|
||||||
|
conn.handle(&encode(OPEN, requested_id, br#"{"cols":100,"rows":30,"shell":"/bin/sh"}"#));
|
||||||
|
let resp = decode(&rx.blocking_recv().unwrap()).unwrap();
|
||||||
|
assert_eq!(resp.kind, OPEN_RESP);
|
||||||
|
assert_eq!(json(&resp.payload)["code"], 0);
|
||||||
|
assert_ne!(resp.session, 0);
|
||||||
|
if requested_id != 0 { assert_eq!(resp.session, requested_id); }
|
||||||
|
|
||||||
|
let (list_tx, mut list_rx) = tokio::sync::mpsc::unbounded_channel();
|
||||||
|
TerminalConn::new(list_tx).handle(&encode(LIST_REQ, 7, &[]));
|
||||||
|
let list = decode(&list_rx.try_recv().unwrap()).unwrap();
|
||||||
|
let sessions: Vec<serde_json::Value> = serde_json::from_slice(&list.payload).unwrap();
|
||||||
|
let session = sessions.iter().find(|s| s["session_id"] == resp.session).unwrap();
|
||||||
|
assert_eq!(session["shell"], "/bin/sh");
|
||||||
|
assert_eq!(session["state"], "running");
|
||||||
|
|
||||||
|
conn.handle(&encode(STDIN, resp.session, b"echo uu-term-ok; exit 3\n"));
|
||||||
|
let mut seen = Vec::new();
|
||||||
|
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
|
||||||
|
let mut exit = None;
|
||||||
|
while std::time::Instant::now() < deadline && exit.is_none() {
|
||||||
|
if let Ok(m) = rx.try_recv() {
|
||||||
|
let f = decode(&m).unwrap();
|
||||||
|
assert_eq!(f.session, resp.session);
|
||||||
|
if f.kind == STDOUT { seen.extend(f.payload) } else if f.kind == EXIT { exit = Some(json(&f.payload)["exit_code"].clone()) }
|
||||||
|
} else { std::thread::sleep(std::time::Duration::from_millis(20)); }
|
||||||
|
}
|
||||||
|
assert!(String::from_utf8_lossy(&seen).contains("uu-term-ok"));
|
||||||
|
assert_eq!(exit, Some(serde_json::json!(3)));
|
||||||
}
|
}
|
||||||
assert!(String::from_utf8_lossy(&seen).contains("uu-term-ok"));
|
|
||||||
assert_eq!(exit, Some(serde_json::json!(3)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+292
@@ -0,0 +1,292 @@
|
|||||||
|
//! Kernel-level virtual keyboard and pointer through /dev/uinput (as Sunshine does).
|
||||||
|
//!
|
||||||
|
//! Works the same under X11, every Wayland compositor and the greeter/lock screen, needs no
|
||||||
|
//! portal permission and sends physical keycodes, so the compositor's keysym → key lookup
|
||||||
|
//! (which can pick an AltGr level for `minus` and drop `_`, issue #3) is not involved.
|
||||||
|
//! Access: a udev rule `KERNEL=="uinput", SUBSYSTEM=="misc", OPTIONS+="static_node=uinput",
|
||||||
|
//! TAG+="uaccess"` grants the logged-in seat user access (see README).
|
||||||
|
//!
|
||||||
|
//! Devices: one keyboard + relative mouse, one absolute pointer (ABS_X/ABS_Y 0..=65535 with
|
||||||
|
//! buttons and wheels, like a QEMU tablet). Mixing relative and absolute axes in one device
|
||||||
|
//! confuses libinput.
|
||||||
|
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
mod imp {
|
||||||
|
use std::fs::{File, OpenOptions};
|
||||||
|
use std::io::Write;
|
||||||
|
use std::os::unix::fs::OpenOptionsExt;
|
||||||
|
use std::os::unix::io::AsRawFd;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
const EV_SYN: u16 = 0x00;
|
||||||
|
const EV_KEY: u16 = 0x01;
|
||||||
|
const EV_REL: u16 = 0x02;
|
||||||
|
const EV_ABS: u16 = 0x03;
|
||||||
|
const SYN_REPORT: u16 = 0;
|
||||||
|
const REL_X: u16 = 0x00;
|
||||||
|
const REL_Y: u16 = 0x01;
|
||||||
|
const REL_HWHEEL: u16 = 0x06;
|
||||||
|
const REL_WHEEL: u16 = 0x08;
|
||||||
|
const ABS_X: u16 = 0x00;
|
||||||
|
const ABS_Y: u16 = 0x01;
|
||||||
|
pub const BTN_LEFT: u16 = 0x110;
|
||||||
|
pub const BTN_RIGHT: u16 = 0x111;
|
||||||
|
pub const BTN_MIDDLE: u16 = 0x112;
|
||||||
|
pub const BTN_SIDE: u16 = 0x113;
|
||||||
|
pub const BTN_EXTRA: u16 = 0x114;
|
||||||
|
pub const ABS_MAX: i32 = 65535;
|
||||||
|
|
||||||
|
const UI_SET_EVBIT: libc::c_ulong = 0x4004_5564;
|
||||||
|
const UI_SET_KEYBIT: libc::c_ulong = 0x4004_5565;
|
||||||
|
const UI_SET_RELBIT: libc::c_ulong = 0x4004_5566;
|
||||||
|
const UI_SET_ABSBIT: libc::c_ulong = 0x4004_5567;
|
||||||
|
const UI_DEV_CREATE: libc::c_ulong = 0x5501;
|
||||||
|
const UI_DEV_DESTROY: libc::c_ulong = 0x5502;
|
||||||
|
const UI_DEV_SETUP: libc::c_ulong = 0x405c_5503;
|
||||||
|
const UI_ABS_SETUP: libc::c_ulong = 0x401c_5504;
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
struct InputId { bustype: u16, vendor: u16, product: u16, version: u16 }
|
||||||
|
#[repr(C)]
|
||||||
|
struct UinputSetup { id: InputId, name: [u8; 80], ff_effects_max: u32 }
|
||||||
|
#[repr(C)]
|
||||||
|
struct AbsInfo { value: i32, minimum: i32, maximum: i32, fuzz: i32, flat: i32, resolution: i32 }
|
||||||
|
#[repr(C)]
|
||||||
|
struct UinputAbsSetup { code: u16, _pad: u16, absinfo: AbsInfo }
|
||||||
|
#[repr(C)]
|
||||||
|
struct InputEvent { time: libc::timeval, kind: u16, code: u16, value: i32 }
|
||||||
|
|
||||||
|
struct Device(File);
|
||||||
|
impl Drop for Device {
|
||||||
|
fn drop(&mut self) { unsafe { libc::ioctl(self.0.as_raw_fd(), UI_DEV_DESTROY); } }
|
||||||
|
}
|
||||||
|
impl Device {
|
||||||
|
fn ioctl(&self, request: libc::c_ulong, value: libc::c_ulong) -> std::io::Result<()> {
|
||||||
|
if unsafe { libc::ioctl(self.0.as_raw_fd(), request, value) } < 0 { Err(std::io::Error::last_os_error()) } else { Ok(()) }
|
||||||
|
}
|
||||||
|
fn create(name: &str, product: u16, configure: impl FnOnce(&Device) -> std::io::Result<()>) -> std::io::Result<Self> {
|
||||||
|
let file = OpenOptions::new().write(true).custom_flags(libc::O_NONBLOCK).open("/dev/uinput")?;
|
||||||
|
let device = Device(file);
|
||||||
|
configure(&device)?;
|
||||||
|
let mut setup = UinputSetup { id: InputId { bustype: 0x06 /* BUS_VIRTUAL */, vendor: 0x1d6b, product, version: 1 }, name: [0; 80], ff_effects_max: 0 };
|
||||||
|
setup.name[..name.len().min(79)].copy_from_slice(&name.as_bytes()[..name.len().min(79)]);
|
||||||
|
if unsafe { libc::ioctl(device.0.as_raw_fd(), UI_DEV_SETUP, &setup) } < 0 { return Err(std::io::Error::last_os_error()); }
|
||||||
|
device.ioctl(UI_DEV_CREATE, 0)?;
|
||||||
|
Ok(device)
|
||||||
|
}
|
||||||
|
fn abs(&self, code: u16) -> std::io::Result<()> {
|
||||||
|
self.ioctl(UI_SET_ABSBIT, code.into())?;
|
||||||
|
let setup = UinputAbsSetup { code, _pad: 0, absinfo: AbsInfo { value: 0, minimum: 0, maximum: ABS_MAX, fuzz: 0, flat: 0, resolution: 0 } };
|
||||||
|
if unsafe { libc::ioctl(self.0.as_raw_fd(), UI_ABS_SETUP, &setup) } < 0 { return Err(std::io::Error::last_os_error()); }
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
fn emit(&mut self, events: &[(u16, u16, i32)]) -> std::io::Result<()> {
|
||||||
|
let zero = libc::timeval { tv_sec: 0, tv_usec: 0 };
|
||||||
|
let mut buffer = Vec::with_capacity((events.len() + 1) * std::mem::size_of::<InputEvent>());
|
||||||
|
for &(kind, code, value) in events.iter().chain(std::iter::once(&(EV_SYN, SYN_REPORT, 0))) {
|
||||||
|
let event = InputEvent { time: zero, kind, code, value };
|
||||||
|
let bytes = unsafe { std::slice::from_raw_parts((&event as *const InputEvent).cast::<u8>(), std::mem::size_of::<InputEvent>()) };
|
||||||
|
buffer.extend_from_slice(bytes);
|
||||||
|
}
|
||||||
|
self.0.write_all(&buffer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Devices { keyboard: Mutex<Device>, pointer: Mutex<Device> }
|
||||||
|
|
||||||
|
impl Devices {
|
||||||
|
pub fn open() -> std::io::Result<Self> {
|
||||||
|
let keyboard = Device::create("uu-remote-linux keyboard", 1, |d| {
|
||||||
|
d.ioctl(UI_SET_EVBIT, EV_KEY.into())?;
|
||||||
|
for code in 1..=248u16 { d.ioctl(UI_SET_KEYBIT, code.into())?; }
|
||||||
|
// Relative motion (games / pointer lock) lives with the keyboard device.
|
||||||
|
d.ioctl(UI_SET_EVBIT, EV_REL.into())?;
|
||||||
|
d.ioctl(UI_SET_RELBIT, REL_X.into())?;
|
||||||
|
d.ioctl(UI_SET_RELBIT, REL_Y.into())?;
|
||||||
|
for button in [BTN_LEFT, BTN_RIGHT, BTN_MIDDLE] { d.ioctl(UI_SET_KEYBIT, button.into())?; }
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
let pointer = Device::create("uu-remote-linux pointer", 2, |d| {
|
||||||
|
d.ioctl(UI_SET_EVBIT, EV_KEY.into())?;
|
||||||
|
for button in [BTN_LEFT, BTN_RIGHT, BTN_MIDDLE, BTN_SIDE, BTN_EXTRA] { d.ioctl(UI_SET_KEYBIT, button.into())?; }
|
||||||
|
d.ioctl(UI_SET_EVBIT, EV_REL.into())?;
|
||||||
|
d.ioctl(UI_SET_RELBIT, REL_WHEEL.into())?;
|
||||||
|
d.ioctl(UI_SET_RELBIT, REL_HWHEEL.into())?;
|
||||||
|
d.ioctl(UI_SET_EVBIT, EV_ABS.into())?;
|
||||||
|
d.abs(ABS_X)?;
|
||||||
|
d.abs(ABS_Y)
|
||||||
|
})?;
|
||||||
|
Ok(Self { keyboard: Mutex::new(keyboard), pointer: Mutex::new(pointer) })
|
||||||
|
}
|
||||||
|
pub fn key(&self, code: u16, pressed: bool) -> std::io::Result<()> {
|
||||||
|
self.keyboard.lock().unwrap().emit(&[(EV_KEY, code, i32::from(pressed))])
|
||||||
|
}
|
||||||
|
pub fn release_all(&self) -> std::io::Result<()> {
|
||||||
|
// Release every supported code on our own devices, even if the
|
||||||
|
// userspace ledger missed a press. Never touch physical devices.
|
||||||
|
let keys:Vec<_>=(1..=248).map(|code|(EV_KEY,code,0))
|
||||||
|
.chain([BTN_LEFT,BTN_RIGHT,BTN_MIDDLE].into_iter().map(|code|(EV_KEY,code,0))).collect();
|
||||||
|
let buttons:Vec<_>=[BTN_LEFT,BTN_RIGHT,BTN_MIDDLE,BTN_SIDE,BTN_EXTRA].into_iter().map(|code|(EV_KEY,code,0)).collect();
|
||||||
|
let keyboard=self.keyboard.lock().unwrap().emit(&keys);
|
||||||
|
let pointer=self.pointer.lock().unwrap().emit(&buttons);
|
||||||
|
keyboard.and(pointer)
|
||||||
|
}
|
||||||
|
pub fn move_absolute(&self, x: i32, y: i32) -> std::io::Result<()> {
|
||||||
|
self.pointer.lock().unwrap().emit(&[(EV_ABS, ABS_X, x.clamp(0, ABS_MAX)), (EV_ABS, ABS_Y, y.clamp(0, ABS_MAX))])
|
||||||
|
}
|
||||||
|
pub fn move_relative(&self, dx: i32, dy: i32) -> std::io::Result<()> {
|
||||||
|
self.keyboard.lock().unwrap().emit(&[(EV_REL, REL_X, dx), (EV_REL, REL_Y, dy)])
|
||||||
|
}
|
||||||
|
pub fn button(&self, code: u16, pressed: bool) -> std::io::Result<()> {
|
||||||
|
self.pointer.lock().unwrap().emit(&[(EV_KEY, code, i32::from(pressed))])
|
||||||
|
}
|
||||||
|
pub fn wheel(&self, vertical: i32, horizontal: i32) -> std::io::Result<()> {
|
||||||
|
let mut events = Vec::new();
|
||||||
|
if vertical != 0 { events.push((EV_REL, REL_WHEEL, vertical)); }
|
||||||
|
if horizontal != 0 { events.push((EV_REL, REL_HWHEEL, horizontal)); }
|
||||||
|
if events.is_empty() { return Ok(()); }
|
||||||
|
self.pointer.lock().unwrap().emit(&events)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod reset_acceptance {
|
||||||
|
use super::*;
|
||||||
|
fn snapshot(device:&Device)->[u8;96] {
|
||||||
|
let mut name=[0u8;80];
|
||||||
|
let request=0x8000_0000u64 | (80<<16) | (b'U' as u64)<<8 | 44;
|
||||||
|
assert!(unsafe {libc::ioctl(device.0.as_raw_fd(),request as libc::c_ulong,name.as_mut_ptr())}>=0);
|
||||||
|
let end=name.iter().position(|v|*v==0).unwrap();
|
||||||
|
let directory=std::path::Path::new("/sys/devices/virtual/input").join(std::str::from_utf8(&name[..end]).unwrap());
|
||||||
|
let event=std::fs::read_dir(directory).unwrap().filter_map(Result::ok)
|
||||||
|
.find(|e|e.file_name().to_string_lossy().starts_with("event")).unwrap();
|
||||||
|
let file=File::open(std::path::Path::new("/dev/input").join(event.file_name())).unwrap();
|
||||||
|
let mut state=[0u8;96];
|
||||||
|
let request=0x8000_0000u64 | (96<<16) | (b'E' as u64)<<8 | 0x18;
|
||||||
|
assert!(unsafe {libc::ioctl(file.as_raw_fd(),request as libc::c_ulong,state.as_mut_ptr())}>=0);
|
||||||
|
state
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
#[ignore="requires isolated QEMU VM uinput and evdev permissions"]
|
||||||
|
fn kernel_devices_release_even_without_an_inputinjector_ledger() {
|
||||||
|
assert_eq!(std::fs::read_to_string("/proc/sys/kernel/hostname").unwrap().trim(),"uu-hyprland-test");
|
||||||
|
assert_eq!(std::process::Command::new("systemd-detect-virt").output().unwrap().stdout,b"qemu\n");
|
||||||
|
let devices=Devices::open().unwrap();
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(500));
|
||||||
|
devices.key(42,true).unwrap();devices.key(30,true).unwrap();
|
||||||
|
devices.button(BTN_LEFT,true).unwrap();
|
||||||
|
assert!(snapshot(&devices.keyboard.lock().unwrap()).iter().any(|v|*v!=0));
|
||||||
|
assert!(snapshot(&devices.pointer.lock().unwrap()).iter().any(|v|*v!=0));
|
||||||
|
devices.release_all().unwrap();
|
||||||
|
assert!(snapshot(&devices.keyboard.lock().unwrap()).iter().all(|v|*v==0));
|
||||||
|
assert!(snapshot(&devices.pointer.lock().unwrap()).iter().all(|v|*v==0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
|
mod imp {
|
||||||
|
pub const BTN_LEFT: u16 = 0x110;
|
||||||
|
pub const BTN_RIGHT: u16 = 0x111;
|
||||||
|
pub const BTN_MIDDLE: u16 = 0x112;
|
||||||
|
pub const BTN_SIDE: u16 = 0x113;
|
||||||
|
pub const BTN_EXTRA: u16 = 0x114;
|
||||||
|
pub const ABS_MAX: i32 = 65535;
|
||||||
|
pub struct Devices;
|
||||||
|
impl Devices {
|
||||||
|
pub fn open() -> std::io::Result<Self> { Err(std::io::Error::new(std::io::ErrorKind::Unsupported, "uinput requires Linux")) }
|
||||||
|
pub fn release_all(&self) -> std::io::Result<()> { Ok(()) }
|
||||||
|
pub fn key(&self, _: u16, _: bool) -> std::io::Result<()> { Ok(()) }
|
||||||
|
pub fn move_absolute(&self, _: i32, _: i32) -> std::io::Result<()> { Ok(()) }
|
||||||
|
pub fn move_relative(&self, _: i32, _: i32) -> std::io::Result<()> { Ok(()) }
|
||||||
|
pub fn button(&self, _: u16, _: bool) -> std::io::Result<()> { Ok(()) }
|
||||||
|
pub fn wheel(&self, _: i32, _: i32) -> std::io::Result<()> { Ok(()) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub use imp::{Devices, ABS_MAX, BTN_EXTRA, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT, BTN_SIDE};
|
||||||
|
|
||||||
|
/// X11 button ordinal (1 left, 2 middle, 3 right, 8/9 side) → evdev button code.
|
||||||
|
pub fn button_code(x11_button: i32) -> Option<u16> {
|
||||||
|
match x11_button { 1 => Some(BTN_LEFT), 2 => Some(BTN_MIDDLE), 3 => Some(BTN_RIGHT), 8 => Some(BTN_SIDE), 9 => Some(BTN_EXTRA), _ => None }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// X11 wheel "buttons" 4/5 (vertical) and 6/7 (horizontal) → (REL_WHEEL, REL_HWHEEL) steps.
|
||||||
|
pub fn wheel_step(x11_button: i32) -> Option<(i32, i32)> {
|
||||||
|
match x11_button { 4 => Some((1, 0)), 5 => Some((-1, 0)), 6 => Some((0, -1)), 7 => Some((0, 1)), _ => None }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Base keysym (as produced by input_mapping::windows_vk_to_keysym) → physical evdev keycode
|
||||||
|
/// at its US-layout position. The compositor then applies the user's own layout, exactly as
|
||||||
|
/// for a hardware keyboard.
|
||||||
|
pub fn keysym_to_evdev(keysym: u64) -> Option<u16> {
|
||||||
|
const LETTERS: [u16; 26] = [30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38, 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44];
|
||||||
|
const KEYPAD: [u16; 10] = [82, 79, 80, 81, 75, 76, 77, 71, 72, 73];
|
||||||
|
let code = match keysym {
|
||||||
|
0x61..=0x7a => LETTERS[(keysym - 0x61) as usize],
|
||||||
|
0x31..=0x39 => (keysym - 0x31) as u16 + 2, // 1..9
|
||||||
|
0x30 => 11,
|
||||||
|
0xffb0..=0xffb9 => KEYPAD[(keysym - 0xffb0) as usize],
|
||||||
|
0xffbe..=0xffc7 => (keysym - 0xffbe) as u16 + 59, // F1..F10
|
||||||
|
0xffc8 => 87, 0xffc9 => 88, // F11, F12
|
||||||
|
0xffca..=0xffd5 => (keysym - 0xffca) as u16 + 183, // F13..F24
|
||||||
|
0xff0b => 76, // Clear: keypad 5 without NumLock, as on PC keyboards
|
||||||
|
0xff08 => 14, 0xff09 => 15, 0xff0d => 28, 0xff1b => 1, 0x20 => 57,
|
||||||
|
0xff13 => 119, 0xffe5 => 58, 0xff7f => 69, 0xff14 => 70, 0xff61 => 99,
|
||||||
|
0xffe1 => 42, 0xffe2 => 54, 0xffe3 => 29, 0xffe4 => 97, 0xffe9 => 56, 0xffea => 100,
|
||||||
|
0xffeb => 125, 0xffec => 126, 0xff67 => 127,
|
||||||
|
0xff55 => 104, 0xff56 => 109, 0xff57 => 107, 0xff50 => 102,
|
||||||
|
0xff51 => 105, 0xff52 => 103, 0xff53 => 106, 0xff54 => 108,
|
||||||
|
0xff63 => 110, 0xffff => 111,
|
||||||
|
0xffaa => 55, 0xffab => 78, 0xffac => 121, 0xffad => 74, 0xffae => 83, 0xffaf => 98, 0xff8d => 96,
|
||||||
|
0x3b => 39, 0x3d => 13, 0x2c => 51, 0x2d => 12, 0x2e => 52, 0x2f => 53, 0x60 => 41,
|
||||||
|
0x5b => 26, 0x5c => 43, 0x5d => 27, 0x27 => 40,
|
||||||
|
0x1008ff12 => 113, 0x1008ff11 => 114, 0x1008ff13 => 115,
|
||||||
|
0x1008ff17 => 163, 0x1008ff16 => 165, 0x1008ff15 => 166, 0x1008ff14 => 164,
|
||||||
|
_ => return None,
|
||||||
|
};
|
||||||
|
Some(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Input backend policy: `UU_INPUT_BACKEND=uinput` forces uinput, `native` disables it,
|
||||||
|
/// `auto` (default) uses uinput for Wayland/portal sessions when /dev/uinput is accessible
|
||||||
|
/// and keeps the verified XTest path on X11.
|
||||||
|
pub fn wanted(backend: crate::desktop_backend::DesktopBackend) -> bool {
|
||||||
|
match std::env::var("UU_INPUT_BACKEND").as_deref() {
|
||||||
|
Ok("uinput") => true,
|
||||||
|
Ok("native") => false,
|
||||||
|
_ => backend != crate::desktop_backend::DesktopBackend::X11,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn maps_keysyms_to_physical_us_positions() {
|
||||||
|
assert_eq!(keysym_to_evdev(0x61), Some(30)); // a
|
||||||
|
assert_eq!(keysym_to_evdev(0x7a), Some(44)); // z
|
||||||
|
assert_eq!(keysym_to_evdev(0x30), Some(11)); // 0
|
||||||
|
assert_eq!(keysym_to_evdev(0x31), Some(2)); // 1
|
||||||
|
assert_eq!(keysym_to_evdev(0x2d), Some(12)); // minus: Shift+minus gives '_' (#3)
|
||||||
|
assert_eq!(keysym_to_evdev(0xffe1), Some(42)); // Shift_L
|
||||||
|
assert_eq!(keysym_to_evdev(0xffc9), Some(88)); // F12
|
||||||
|
assert_eq!(keysym_to_evdev(0xffb5), Some(76)); // KP_5
|
||||||
|
assert_eq!(keysym_to_evdev(0x12345), None);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn every_mapped_windows_vk_has_a_physical_key() {
|
||||||
|
let missing: Vec<i32> = (0..=0xff).filter(|vk| {
|
||||||
|
crate::input_mapping::windows_vk_to_keysym(*vk).is_some_and(|ks| keysym_to_evdev(ks).is_none())
|
||||||
|
}).collect();
|
||||||
|
assert!(missing.is_empty(), "VKs without evdev mapping: {missing:x?}");
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn buttons_and_wheel() {
|
||||||
|
assert_eq!(button_code(1), Some(BTN_LEFT));
|
||||||
|
assert_eq!(button_code(3), Some(BTN_RIGHT));
|
||||||
|
assert_eq!(wheel_step(4), Some((1, 0)));
|
||||||
|
assert_eq!(wheel_step(7), Some((0, 1)));
|
||||||
|
assert_eq!(button_code(4), None);
|
||||||
|
}
|
||||||
|
}
|
||||||
+254
-34
@@ -1,4 +1,5 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::sync::atomic::AtomicBool;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
use tokio::io::AsyncReadExt;
|
use tokio::io::AsyncReadExt;
|
||||||
use tokio::process::Command;
|
use tokio::process::Command;
|
||||||
@@ -11,10 +12,20 @@ use crate::capture_setting::VideoSettings;
|
|||||||
|
|
||||||
/// Default frame rate; the viewer's 帧率 setting overrides it per connection.
|
/// Default frame rate; the viewer's 帧率 setting overrides it per connection.
|
||||||
pub const FPS: u64 = 60;
|
pub const FPS: u64 = 60;
|
||||||
/// IDR every 0.5s. All-intra 60fps measured 30 Mbit/s on the real desktop (saturating
|
/// Keyframe interval. All-intra 60fps measured 30 Mbit/s on the real desktop (saturating the
|
||||||
/// the viewer's Wi-Fi: 3-6 fps received); GOP 30 measured 2.7 Mbit/s at the same CQ18.
|
/// viewer's Wi-Fi: 3-6 fps received). On a static desktop with NVENC CQ18 a 0.5s GOP measured
|
||||||
/// A broken prediction chain therefore freezes at most this long.
|
/// 7.7 Mbit/s and a 2s GOP 1.9 Mbit/s. A broken prediction chain freezes until the next IDR or
|
||||||
const GOP: u64 = 30;
|
/// a PLI. Only the in-process encoder answers PLI/FIR with an IDR, so the child-process path
|
||||||
|
/// keeps the short 0.5s GOP and the longer 2s GOP is used only where an on-demand IDR exists.
|
||||||
|
const GOP_SECONDS_PROCESS: f64 = 0.5;
|
||||||
|
const GOP_SECONDS_IN_PROCESS: f64 = 2.0;
|
||||||
|
|
||||||
|
/// Keyframe interval in frames at the nominal rate. `UU_VIDEO_GOP_SECONDS` overrides the default for both paths.
|
||||||
|
fn gop_frames(fps: u32, in_process: bool) -> u64 {
|
||||||
|
let default = if in_process { GOP_SECONDS_IN_PROCESS } else { GOP_SECONDS_PROCESS };
|
||||||
|
let seconds = std::env::var("UU_VIDEO_GOP_SECONDS").ok().and_then(|v| v.parse::<f64>().ok()).unwrap_or(default);
|
||||||
|
((f64::from(fps) * seconds).round() as u64).max(1)
|
||||||
|
}
|
||||||
const MAX_ACCESS_UNIT: usize = 8 * 1024 * 1024;
|
const MAX_ACCESS_UNIT: usize = 8 * 1024 * 1024;
|
||||||
|
|
||||||
fn nal_offsets(bytes: &[u8], kind: u8) -> Vec<usize> {
|
fn nal_offsets(bytes: &[u8], kind: u8) -> Vec<usize> {
|
||||||
@@ -42,9 +53,36 @@ struct CompleteFrame {
|
|||||||
|
|
||||||
const RTP_CLOCK: u64 = 90_000;
|
const RTP_CLOCK: u64 = 90_000;
|
||||||
|
|
||||||
/// `UU_VIDEO_ENCODER=cpu` keeps the virtual CPU display entirely off the GPU.
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
pub fn gpu_encoder_requested() -> bool {
|
pub(crate) enum Encoder { Cpu, Nvenc, Vaapi }
|
||||||
std::env::var("UU_VIDEO_ENCODER").map_or(true, |v| v != "cpu")
|
impl Encoder {
|
||||||
|
pub(crate) fn requested() -> Self { Self::from_setting(std::env::var("UU_VIDEO_ENCODER").ok().as_deref(), nvidia_present()) }
|
||||||
|
/// Unset/auto tries NVENC only with an NVIDIA driver loaded: `ffmpeg -encoders` lists h264_nvenc
|
||||||
|
/// on any machine, so without this every stream start first ran a doomed NVENC process (#10).
|
||||||
|
fn from_setting(value: Option<&str>, nvidia: bool) -> Self {
|
||||||
|
match value { Some("cpu") => Self::Cpu, Some("vaapi") => Self::Vaapi, Some("nvenc") => Self::Nvenc,
|
||||||
|
_ => if nvidia { Self::Nvenc } else { Self::Cpu } }
|
||||||
|
}
|
||||||
|
pub(crate) fn name(self) -> &'static str { match self { Self::Cpu => "libx264", Self::Nvenc => "h264_nvenc", Self::Vaapi => "h264_vaapi" } }
|
||||||
|
}
|
||||||
|
fn nvidia_present() -> bool {
|
||||||
|
std::path::Path::new("/proc/driver/nvidia/version").exists() || std::path::Path::new("/dev/nvidia0").exists()
|
||||||
|
}
|
||||||
|
pub(crate) fn vaapi_device() -> String { std::env::var("UU_VAAPI_DEVICE").unwrap_or_else(|_| "/dev/dri/renderD128".into()) }
|
||||||
|
|
||||||
|
/// NV12 conversion must precede upload; scaling runs on software frames, before hwupload.
|
||||||
|
fn vaapi_filter(output: Option<(u32, u32)>) -> String {
|
||||||
|
let scale = output.map_or(String::new(), |(w,h)| format!("scale={w}:{h}:flags=bilinear,"));
|
||||||
|
format!("{scale}format=nv12,hwupload")
|
||||||
|
}
|
||||||
|
fn vaapi_options(settings: &VideoSettings, gop: &str) -> Vec<String> {
|
||||||
|
let (maxrate, buffer) = rate_args(settings, Encoder::Vaapi);
|
||||||
|
// QVBR is not uniformly available on Intel/AMD drivers. VBR + qmin sets a quality
|
||||||
|
// floor while retaining the viewer's bitrate cap; unsupported drivers fall back to CPU.
|
||||||
|
["-c:v", "h264_vaapi", "-profile:v", "constrained_baseline", "-g", gop, "-bf", "0",
|
||||||
|
"-rc_mode", "VBR", "-b:v", &settings.max_kbps().map_or("16M".into(), |k| format!("{}k",k*3/4)),
|
||||||
|
"-maxrate", &maxrate, "-bufsize", &buffer, "-qmin", &settings.cq().to_string(), "-async_depth", "1", "-aud", "1"]
|
||||||
|
.into_iter().map(str::to_owned).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Round UP (+1ns float guard) so webrtc's floor(duration*90000) yields exactly `ticks`.
|
/// Round UP (+1ns float guard) so webrtc's floor(duration*90000) yields exactly `ticks`.
|
||||||
@@ -89,53 +127,119 @@ impl DependencyGate{
|
|||||||
// Aborting the owner task drops Child, whose kill_on_drop kills only this encoder.
|
// Aborting the owner task drops Child, whose kill_on_drop kills only this encoder.
|
||||||
struct EncoderTask(tokio::task::JoinHandle<Result<(), String>>);
|
struct EncoderTask(tokio::task::JoinHandle<Result<(), String>>);
|
||||||
impl Drop for EncoderTask { fn drop(&mut self) { self.0.abort(); } }
|
impl Drop for EncoderTask { fn drop(&mut self) { self.0.abort(); } }
|
||||||
|
impl EncoderTask {
|
||||||
|
async fn restart(&mut self, future: impl std::future::Future<Output = Result<(), String>> + Send + 'static) {
|
||||||
|
// abort() alone can leave the old producer running until its next yield.
|
||||||
|
// Finish it before a new IDR can be followed by an old encoder's P-frame.
|
||||||
|
self.0.abort();
|
||||||
|
let _ = (&mut self.0).await;
|
||||||
|
self.0 = tokio::spawn(future);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "inproc-av")]
|
||||||
|
struct StopOnDrop(Arc<AtomicBool>);
|
||||||
|
#[cfg(feature = "inproc-av")]
|
||||||
|
impl Drop for StopOnDrop { fn drop(&mut self) { self.0.store(true, std::sync::atomic::Ordering::Relaxed); } }
|
||||||
|
|
||||||
async fn read_encoder(
|
async fn read_encoder(
|
||||||
size: (u32, u32), display: String, portal:Option<Arc<crate::portal::PortalSession>>,
|
size: (u32, u32), display: String, portal:Option<Arc<crate::portal::PortalSession>>,
|
||||||
latest: mpsc::Sender<Arc<CompleteFrame>>,
|
latest: mpsc::Sender<Arc<CompleteFrame>>,
|
||||||
settings: VideoSettings, sequence: Arc<std::sync::atomic::AtomicU64>,
|
settings: VideoSettings, sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
// The sequence is shared so a CPU fallback or settings restart continues it (skip statistics stay correct).
|
// The sequence is shared so a CPU fallback or settings restart continues it (skip statistics stay correct).
|
||||||
if !gpu_encoder_requested() { return read_encoder_with(size,display,portal,latest,false,sequence,settings).await; }
|
let encoder = Encoder::requested();
|
||||||
match read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),true,sequence.clone(),settings).await {
|
if encoder == Encoder::Cpu { return read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr).await; }
|
||||||
|
match read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),encoder,sequence.clone(),settings,idr.clone()).await {
|
||||||
Ok(())=>Ok(()),
|
Ok(())=>Ok(()),
|
||||||
Err(error) if !latest.is_closed()=>{
|
Err(error) if !latest.is_closed()=>{
|
||||||
log::warn!("NVENC stream unavailable; switching scoped encoder to CPU: {}",error);
|
log::warn!("{} stream unavailable; switching scoped encoder to CPU: {}",encoder.name(),error);
|
||||||
read_encoder_with(size,display,portal,latest,false,sequence,settings).await
|
read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr).await
|
||||||
},
|
},
|
||||||
Err(error)=>Err(error),
|
Err(error)=>Err(error),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Capture and encode inside this process; each packet is one access unit.
|
||||||
|
#[cfg(feature = "inproc-av")]
|
||||||
|
async fn read_inprocess(
|
||||||
|
config: crate::inproc_encoder::Config, latest: mpsc::Sender<Arc<CompleteFrame>>,
|
||||||
|
sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
let (units_tx, mut units) = mpsc::channel::<Vec<u8>>(4);
|
||||||
|
let stop = Arc::new(AtomicBool::new(false));
|
||||||
|
// If this future is dropped (reader task aborted), the blocking encoder stops at its next packet.
|
||||||
|
let _stop_on_drop = StopOnDrop(stop.clone());
|
||||||
|
let job = tokio::task::spawn_blocking({
|
||||||
|
let stop = stop.clone();
|
||||||
|
move || crate::inproc_encoder::run(config, units_tx, stop, idr)
|
||||||
|
});
|
||||||
|
let mut gate = DependencyGate::default();
|
||||||
|
while let Some(data) = units.recv().await {
|
||||||
|
let sequence = sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1;
|
||||||
|
gate.submit(&latest, Arc::new(CompleteFrame { sequence, completed_at: Instant::now(), data }));
|
||||||
|
if latest.is_closed() { break; }
|
||||||
|
}
|
||||||
|
// Dropping the receiver also unblocks the encoder if it is waiting to send.
|
||||||
|
stop.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||||
|
drop(units);
|
||||||
|
job.await.map_err(|e| format!("Encoder task: {e}"))?
|
||||||
|
}
|
||||||
|
|
||||||
/// Rate-control arguments: constant quality capped at 30M (自动), or capped at the viewer's
|
/// Rate-control arguments: constant quality capped at 30M (自动), or capped at the viewer's
|
||||||
/// chosen bitrate. The VBV buffer stays at most 500k so a cap never adds queueing latency.
|
/// chosen bitrate. The VBV buffer stays at most 500k so a cap never adds queueing latency.
|
||||||
fn rate_args(settings: &VideoSettings) -> (String, String) {
|
/// VBV buffer in kbit. NVENC ull and VAAPI keep the small low-latency buffer; libx264 needs room for a
|
||||||
|
/// whole IDR frame, otherwise large desktops get near-grey keyframes that sharpen over the GOP (#8).
|
||||||
|
/// Half a second of bitrate still starved repeated 0.5 s-GOP keyframes at 3296x2036/2M; one second did not.
|
||||||
|
fn buffer_kbits(max_kbps: u32, encoder: Encoder) -> u32 {
|
||||||
|
if encoder == Encoder::Cpu { max_kbps.clamp(500, 8000) } else { (max_kbps / 8).clamp(100, 500) }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rate_args(settings: &VideoSettings, encoder: Encoder) -> (String, String) {
|
||||||
let max_kbps = settings.max_kbps().unwrap_or(30_000);
|
let max_kbps = settings.max_kbps().unwrap_or(30_000);
|
||||||
let buffer_kbits = (max_kbps / 8).clamp(100, 500);
|
(format!("{max_kbps}k"), format!("{}k", buffer_kbits(max_kbps, encoder)))
|
||||||
(format!("{max_kbps}k"), format!("{buffer_kbits}k"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn read_encoder_with(
|
async fn read_encoder_with(
|
||||||
size: (u32,u32),display:String,portal:Option<Arc<crate::portal::PortalSession>>,latest:mpsc::Sender<Arc<CompleteFrame>>,gpu:bool,sequence:Arc<std::sync::atomic::AtomicU64>,
|
size: (u32,u32),display:String,portal:Option<Arc<crate::portal::PortalSession>>,latest:mpsc::Sender<Arc<CompleteFrame>>,encoder:Encoder,sequence:Arc<std::sync::atomic::AtomicU64>,
|
||||||
mut settings: VideoSettings,
|
settings: VideoSettings, #[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] idr: Arc<AtomicBool>,
|
||||||
)->Result<(),String>{
|
)->Result<(),String>{
|
||||||
|
let kms=display.starts_with("kms:");
|
||||||
|
let wayland=kms || display.starts_with("wayland:");
|
||||||
|
let kms_capture=if kms {
|
||||||
|
let capture=tokio::task::spawn_blocking(crate::kms::Capture::open).await.map_err(|_|"KMS worker failed")??;
|
||||||
|
if (capture.width,capture.height)!=size { return Err("KMS mode changed; restart host to refresh desktop geometry".into()); }
|
||||||
|
Some(Arc::new(capture))
|
||||||
|
} else { None };
|
||||||
|
#[cfg(feature = "inproc-av")]
|
||||||
|
if !wayland && portal.is_none() && crate::inproc_encoder::selected() {
|
||||||
|
let max_kbps = settings.max_kbps().unwrap_or(30_000) as i64;
|
||||||
|
let config = crate::inproc_encoder::Config {
|
||||||
|
display: display.clone(), input: size, output: settings.output, fps: u32::from(settings.fps),
|
||||||
|
gop: gop_frames(settings.fps, true) as u32, cq: settings.cq() as u32,
|
||||||
|
target_bps: settings.max_kbps().map_or(16_000, |k| i64::from(k) * 3 / 4) * 1000,
|
||||||
|
max_bps: max_kbps * 1000, buffer_bits: i64::from(buffer_kbits(max_kbps as u32, encoder)) * 1000, encoder,
|
||||||
|
damage: crate::inproc_encoder::damage_enabled(),
|
||||||
|
};
|
||||||
|
log::info!("Desktop encoder: display={} source={}x{} output={:?} fps={} cq={} maxrate={}k encoder={} pipeline=in-process",
|
||||||
|
display,size.0,size.1,settings.output,settings.fps,config.cq,max_kbps,encoder.name());
|
||||||
|
return read_inprocess(config, latest, sequence, idr).await;
|
||||||
|
}
|
||||||
let size = format!("{}x{}", size.0, size.1);
|
let size = format!("{}x{}", size.0, size.1);
|
||||||
let wayland=display.starts_with("wayland:");
|
|
||||||
// The separate wayland-capture process produces frames at its own fixed rate.
|
|
||||||
if wayland && portal.is_none() { settings.fps = FPS as u32; }
|
|
||||||
let fps = settings.fps.to_string();
|
let fps = settings.fps.to_string();
|
||||||
// Keep the IDR interval at 0.5 s whatever the frame rate.
|
// Keep the IDR interval at 2 s whatever the frame rate.
|
||||||
let gop = (u64::from(settings.fps) * GOP / FPS).max(1).to_string();
|
let gop = gop_frames(settings.fps, false).to_string();
|
||||||
let (maxrate, bufsize) = rate_args(&settings);
|
let (maxrate, bufsize) = rate_args(&settings, encoder);
|
||||||
let cq = settings.cq().to_string();
|
let cq = settings.cq().to_string();
|
||||||
let target = settings.max_kbps().map_or("16M".to_string(), |k| format!("{}k", k * 3 / 4));
|
let target = settings.max_kbps().map_or("16M".to_string(), |k| format!("{}k", k * 3 / 4));
|
||||||
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,if gpu{"h264_nvenc"}else{"libx264"});
|
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,encoder.name());
|
||||||
// A bundled FFmpeg prefix (config ffmpeg_root / UU_FFMPEG_ROOT) or `ffmpeg` on PATH.
|
// A bundled FFmpeg prefix (config ffmpeg_root / UU_FFMPEG_ROOT) or `ffmpeg` on PATH.
|
||||||
let mut command=match crate::config::ffmpeg_root() {
|
let mut command=match crate::config::ffmpeg_root() {
|
||||||
Some(root) => { let mut c=Command::new(root.join("bin/ffmpeg")); c.env("LD_LIBRARY_PATH",root.join("lib/x86_64-linux-gnu")); c }
|
Some(root) => { let mut c=Command::new(root.join("bin/ffmpeg")); c.env("LD_LIBRARY_PATH",root.join("lib/x86_64-linux-gnu")); c }
|
||||||
None => Command::new("ffmpeg"),
|
None => Command::new("ffmpeg"),
|
||||||
};
|
};
|
||||||
command.args(["-hide_banner","-loglevel","error","-nostdin"]);
|
command.args(["-hide_banner","-loglevel","error","-nostdin"]);
|
||||||
|
if encoder == Encoder::Vaapi { command.args(["-vaapi_device", &vaapi_device()]); }
|
||||||
if wayland {
|
if wayland {
|
||||||
command.args(["-f","rawvideo","-pixel_format","bgra","-video_size",&size,
|
command.args(["-f","rawvideo","-pixel_format","bgra","-video_size",&size,
|
||||||
"-framerate",&fps,"-i","pipe:0","-an"]);
|
"-framerate",&fps,"-i","pipe:0","-an"]);
|
||||||
@@ -145,10 +249,14 @@ async fn read_encoder_with(
|
|||||||
"-video_size",&size,"-i",&display,"-an"]);
|
"-video_size",&size,"-i",&display,"-an"]);
|
||||||
}
|
}
|
||||||
// 其他分辨率: scale the captured desktop (the host display mode is left untouched).
|
// 其他分辨率: scale the captured desktop (the host display mode is left untouched).
|
||||||
if let Some((width, height)) = settings.output {
|
if encoder == Encoder::Vaapi {
|
||||||
|
command.args(["-vf", &vaapi_filter(settings.output)]);
|
||||||
|
} else if let Some((width, height)) = settings.output {
|
||||||
command.args(["-vf", &format!("scale={width}:{height}:flags=bilinear")]);
|
command.args(["-vf", &format!("scale={width}:{height}:flags=bilinear")]);
|
||||||
}
|
}
|
||||||
if gpu{
|
if encoder == Encoder::Vaapi {
|
||||||
|
command.args(vaapi_options(&settings, &gop));
|
||||||
|
} else if encoder == Encoder::Nvenc {
|
||||||
command.args(["-c:v","h264_nvenc","-preset","p4","-tune","ull",
|
command.args(["-c:v","h264_nvenc","-preset","p4","-tune","ull",
|
||||||
"-profile:v","baseline","-pix_fmt","yuv420p","-g",&gop,"-bf","0",
|
"-profile:v","baseline","-pix_fmt","yuv420p","-g",&gop,"-bf","0",
|
||||||
"-rc","vbr","-cq",&cq,"-b:v",&target,"-maxrate",&maxrate,"-bufsize",&bufsize,
|
"-rc","vbr","-cq",&cq,"-b:v",&target,"-maxrate",&maxrate,"-bufsize",&bufsize,
|
||||||
@@ -172,7 +280,23 @@ async fn read_encoder_with(
|
|||||||
.stderr(std::process::Stdio::inherit())
|
.stderr(std::process::Stdio::inherit())
|
||||||
.spawn().map_err(|e| format!("Start scoped encoder: {e}"))?;
|
.spawn().map_err(|e| format!("Start scoped encoder: {e}"))?;
|
||||||
// Scope capture to this encoder. Dropping either task kills its child.
|
// Scope capture to this encoder. Dropping either task kills its child.
|
||||||
let _capture=if let Some(session)=portal {
|
let _capture=if let Some(capture)=kms_capture {
|
||||||
|
let mut sink=child.stdin.take().ok_or("Encoder stdin missing")?;
|
||||||
|
Some(EncoderTask(tokio::spawn(async move {
|
||||||
|
use tokio::io::AsyncWriteExt;
|
||||||
|
let mut tick=tokio::time::interval(Duration::from_nanos(1_000_000_000/u64::from(settings.fps)));
|
||||||
|
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||||
|
loop {
|
||||||
|
tick.tick().await;
|
||||||
|
let capture=capture.clone();
|
||||||
|
let bytes=tokio::task::spawn_blocking(move || {
|
||||||
|
let mut bytes=vec![0;capture.width as usize*capture.height as usize*4];
|
||||||
|
capture.capture(&mut bytes)?; Ok::<_,String>(bytes)
|
||||||
|
}).await.map_err(|_|"KMS worker failed")??;
|
||||||
|
sink.write_all(&bytes).await.map_err(|_|"KMS encoder pipe failed")?;
|
||||||
|
}
|
||||||
|
})))
|
||||||
|
} else if let Some(session)=portal {
|
||||||
let mut sink=child.stdin.take().ok_or("Encoder stdin missing")?;
|
let mut sink=child.stdin.take().ok_or("Encoder stdin missing")?;
|
||||||
Some(EncoderTask(tokio::spawn(async move {
|
Some(EncoderTask(tokio::spawn(async move {
|
||||||
use tokio::io::AsyncWriteExt;
|
use tokio::io::AsyncWriteExt;
|
||||||
@@ -197,7 +321,7 @@ async fn read_encoder_with(
|
|||||||
let executable=std::path::PathBuf::from("/proc/self/exe");
|
let executable=std::path::PathBuf::from("/proc/self/exe");
|
||||||
#[cfg(not(target_os="linux"))]
|
#[cfg(not(target_os="linux"))]
|
||||||
let executable=std::env::current_exe().map_err(|e|format!("Capture executable: {e}"))?;
|
let executable=std::env::current_exe().map_err(|e|format!("Capture executable: {e}"))?;
|
||||||
let mut capture=Command::new(executable).arg("wayland-capture")
|
let mut capture=wayland_capture_command(&executable,settings.fps)
|
||||||
.kill_on_drop(true).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::inherit())
|
.kill_on_drop(true).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::inherit())
|
||||||
.spawn().map_err(|e|format!("Start Wayland capture: {e}"))?;
|
.spawn().map_err(|e|format!("Start Wayland capture: {e}"))?;
|
||||||
let mut source=capture.stdout.take().ok_or("Capture stdout missing")?;
|
let mut source=capture.stdout.take().ok_or("Capture stdout missing")?;
|
||||||
@@ -233,6 +357,12 @@ async fn read_encoder_with(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn wayland_capture_command(executable:&std::path::Path,fps:u32)->Command {
|
||||||
|
let mut command=Command::new(executable);
|
||||||
|
command.arg("wayland-capture").env("UU_WAYLAND_FPS",fps.to_string());
|
||||||
|
command
|
||||||
|
}
|
||||||
|
|
||||||
fn transport_closed(pc: &RTCPeerConnection) -> bool {
|
fn transport_closed(pc: &RTCPeerConnection) -> bool {
|
||||||
matches!(pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed)
|
matches!(pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed)
|
||||||
|| pc.sctp().state() == webrtc::sctp_transport::sctp_transport_state::RTCSctpTransportState::Closed
|
|| pc.sctp().state() == webrtc::sctp_transport::sctp_transport_state::RTCSctpTransportState::Closed
|
||||||
@@ -247,11 +377,15 @@ pub async fn probe(size:(u32,u32),display:String,portal:Option<Arc<crate::portal
|
|||||||
};
|
};
|
||||||
let (tx,mut rx)=mpsc::channel(4);
|
let (tx,mut rx)=mpsc::channel(4);
|
||||||
let sequence=Arc::new(std::sync::atomic::AtomicU64::new(0));
|
let sequence=Arc::new(std::sync::atomic::AtomicU64::new(0));
|
||||||
let _reader=EncoderTask(tokio::spawn(read_encoder(size,display,portal,tx,VideoSettings::default(),sequence)));
|
let fps=crate::wayland::capture_fps(std::env::var("UU_PROBE_FPS").ok().as_deref())?;
|
||||||
|
let settings=VideoSettings {fps,..VideoSettings::default()};
|
||||||
|
let _reader=EncoderTask(tokio::spawn(read_encoder(size,display,portal,tx,settings,sequence,Arc::new(AtomicBool::new(false)))));
|
||||||
let mut count=0;
|
let mut count=0;
|
||||||
let mut bytes=0;
|
let mut bytes=0;
|
||||||
tokio::time::timeout(Duration::from_secs(15),async {
|
// UU_PROBE_FRAMES lengthens the sample for baseline measurements; default is 5 frames.
|
||||||
while count<5 {
|
let wanted:u64=std::env::var("UU_PROBE_FRAMES").ok().and_then(|v|v.parse().ok()).unwrap_or(5);
|
||||||
|
tokio::time::timeout(Duration::from_secs(15+wanted/30),async {
|
||||||
|
while count<wanted {
|
||||||
let frame=rx.recv().await.ok_or("Video probe encoder ended")?;
|
let frame=rx.recv().await.ok_or("Video probe encoder ended")?;
|
||||||
if count==0 && !is_independent_frame(&frame.data) { return Err("Video probe did not begin with IDR".to_owned()); }
|
if count==0 && !is_independent_frame(&frame.data) { return Err("Video probe did not begin with IDR".to_owned()); }
|
||||||
if let Some(file)=file.as_mut() { file.write_all(&frame.data).map_err(|e|e.to_string())?; }
|
if let Some(file)=file.as_mut() { file.write_all(&frame.data).map_err(|e|e.to_string())?; }
|
||||||
@@ -264,7 +398,7 @@ pub async fn probe(size:(u32,u32),display:String,portal:Option<Arc<crate::portal
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStaticSample>, size: (u32, u32), display: String, portal:Option<Arc<crate::portal::PortalSession>>,
|
pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStaticSample>, size: (u32, u32), display: String, portal:Option<Arc<crate::portal::PortalSession>>,
|
||||||
mut settings: tokio::sync::watch::Receiver<VideoSettings>) -> Result<(), String> {
|
mut settings: tokio::sync::watch::Receiver<VideoSettings>, idr: Arc<AtomicBool>) -> Result<(), String> {
|
||||||
loop {
|
loop {
|
||||||
if transport_closed(&pc) { return Ok(()); }
|
if transport_closed(&pc) { return Ok(()); }
|
||||||
if pc.connection_state() == RTCPeerConnectionState::Connected
|
if pc.connection_state() == RTCPeerConnectionState::Connected
|
||||||
@@ -275,7 +409,7 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalSta
|
|||||||
let (latest, mut receiver) = mpsc::channel(4);
|
let (latest, mut receiver) = mpsc::channel(4);
|
||||||
let sequence = Arc::new(std::sync::atomic::AtomicU64::new(0));
|
let sequence = Arc::new(std::sync::atomic::AtomicU64::new(0));
|
||||||
let current = *settings.borrow_and_update();
|
let current = *settings.borrow_and_update();
|
||||||
let mut reader = EncoderTask(tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone())));
|
let mut reader = EncoderTask(tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone(), idr.clone())));
|
||||||
let mut previous = None;
|
let mut previous = None;
|
||||||
let mut timeline = RtpTimeline { fps: u64::from(current.fps), ..Default::default() };
|
let mut timeline = RtpTimeline { fps: u64::from(current.fps), ..Default::default() };
|
||||||
let mut settings_open = true;
|
let mut settings_open = true;
|
||||||
@@ -296,7 +430,7 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalSta
|
|||||||
// still queued from the old encoder remain a valid chain before it.
|
// still queued from the old encoder remain a valid chain before it.
|
||||||
let next = *settings.borrow_and_update();
|
let next = *settings.borrow_and_update();
|
||||||
timeline.fps = u64::from(next.fps);
|
timeline.fps = u64::from(next.fps);
|
||||||
reader = EncoderTask(tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), next, sequence.clone())));
|
reader.restart(read_encoder(size, display.clone(), portal.clone(), latest.clone(), next, sequence.clone(), idr.clone())).await;
|
||||||
},
|
},
|
||||||
frame = receiver.recv() => {
|
frame = receiver.recv() => {
|
||||||
// A closed queue means the reader ended; its JoinHandle carries the reason.
|
// A closed queue means the reader ended; its JoinHandle carries the reason.
|
||||||
@@ -335,8 +469,94 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalSta
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn native_wayland_child_uses_requested_capture_rate() {
|
||||||
|
for fps in [5,30,60,90,144] {
|
||||||
|
let command=super::wayland_capture_command(std::path::Path::new("uuremote-server"),fps);
|
||||||
|
let value=fps.to_string();
|
||||||
|
assert!(command.as_std().get_envs().any(|(k,v)|k=="UU_WAYLAND_FPS" && v==Some(std::ffi::OsStr::new(&value))));
|
||||||
|
}
|
||||||
|
}
|
||||||
use super::*;
|
use super::*;
|
||||||
#[test]
|
#[test]
|
||||||
|
fn x264_buffer_fits_keyframes() {
|
||||||
|
assert_eq!(buffer_kbits(2000, Encoder::Nvenc), 250);
|
||||||
|
assert_eq!(buffer_kbits(2000, Encoder::Vaapi), 250);
|
||||||
|
assert_eq!(buffer_kbits(2000, Encoder::Cpu), 2000);
|
||||||
|
assert_eq!(buffer_kbits(300, Encoder::Cpu), 500);
|
||||||
|
assert_eq!(buffer_kbits(30_000, Encoder::Cpu), 8000);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn vaapi_is_explicit_and_upload_follows_scale() {
|
||||||
|
assert_eq!(Encoder::from_setting(None, true), Encoder::Nvenc);
|
||||||
|
assert_eq!(Encoder::from_setting(None, false), Encoder::Cpu);
|
||||||
|
assert_eq!(Encoder::from_setting(Some("nvenc"), false), Encoder::Nvenc);
|
||||||
|
assert_eq!(Encoder::from_setting(Some("cpu"), true), Encoder::Cpu);
|
||||||
|
assert_eq!(Encoder::from_setting(Some("vaapi"), true), Encoder::Vaapi);
|
||||||
|
assert_eq!(vaapi_filter(None), "format=nv12,hwupload");
|
||||||
|
assert_eq!(vaapi_filter(Some((1280,720))), "scale=1280:720:flags=bilinear,format=nv12,hwupload");
|
||||||
|
let options = vaapi_options(&VideoSettings::default(), "30");
|
||||||
|
for pair in [["-g","30"], ["-bf","0"], ["-async_depth","1"], ["-aud","1"]] {
|
||||||
|
assert!(options.windows(2).any(|w| w[0] == pair[0] && w[1] == pair[1]));
|
||||||
|
}
|
||||||
|
assert!(!options.iter().any(|v| v == "-r" || v == "-pix_fmt"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn encoder_restart_stops_old_producer_before_starting_new_chain() {
|
||||||
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
struct MarkStopped(Arc<AtomicBool>);
|
||||||
|
impl Drop for MarkStopped {
|
||||||
|
fn drop(&mut self) { self.0.store(true, Ordering::SeqCst); }
|
||||||
|
}
|
||||||
|
let stopped = Arc::new(AtomicBool::new(false));
|
||||||
|
let old_stopped = stopped.clone();
|
||||||
|
let (tx, mut rx) = mpsc::channel(4);
|
||||||
|
let old_tx = tx.clone();
|
||||||
|
let (started_tx, started_rx) = tokio::sync::oneshot::channel();
|
||||||
|
let frame = |sequence, idr| Arc::new(CompleteFrame {
|
||||||
|
sequence, completed_at: Instant::now(),
|
||||||
|
data: vec![0, 0, 1, 9, 240, 0, 0, 1, if idr { 5 } else { 1 }, 42],
|
||||||
|
});
|
||||||
|
let mut reader = EncoderTask(tokio::spawn(async move {
|
||||||
|
let _stopped = MarkStopped(old_stopped);
|
||||||
|
let mut gate = DependencyGate::default();
|
||||||
|
gate.submit(&old_tx, frame(1, true));
|
||||||
|
gate.submit(&old_tx, frame(2, false));
|
||||||
|
started_tx.send(()).unwrap();
|
||||||
|
std::future::pending::<Result<(), String>>().await
|
||||||
|
}));
|
||||||
|
started_rx.await.unwrap();
|
||||||
|
let replacement_stopped = stopped.clone();
|
||||||
|
reader.restart(async move {
|
||||||
|
assert!(replacement_stopped.load(Ordering::SeqCst));
|
||||||
|
DependencyGate::default().submit(&tx, frame(3, true));
|
||||||
|
Ok(())
|
||||||
|
}).await;
|
||||||
|
// On this current-thread runtime, abort without awaiting cannot drop the old
|
||||||
|
// future before returning here. No sleep or scheduler race is needed.
|
||||||
|
assert!(stopped.load(Ordering::SeqCst), "old producer is still alive after restart");
|
||||||
|
(&mut reader.0).await.unwrap().unwrap();
|
||||||
|
for (sequence, idr) in [(1, true), (2, false), (3, true)] {
|
||||||
|
let queued = rx.recv().await.unwrap();
|
||||||
|
assert_eq!(queued.sequence, sequence);
|
||||||
|
assert_eq!(is_independent_frame(&queued.data), idr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn encoder_restart_replaces_an_already_failed_producer() {
|
||||||
|
let mut reader = EncoderTask(tokio::spawn(async { Err("encoder ended".into()) }));
|
||||||
|
while !reader.0.is_finished() { tokio::task::yield_now().await; }
|
||||||
|
let (started_tx, started_rx) = tokio::sync::oneshot::channel();
|
||||||
|
reader.restart(async move {
|
||||||
|
started_tx.send(()).unwrap();
|
||||||
|
Ok(())
|
||||||
|
}).await;
|
||||||
|
started_rx.await.unwrap();
|
||||||
|
(&mut reader.0).await.unwrap().unwrap();
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
fn boundaries_accept_complete_idr_and_reject_dependent_frames() {
|
fn boundaries_accept_complete_idr_and_reject_dependent_frames() {
|
||||||
let bytes = [0,0,0,1,9,240,0,0,1,7,1,0,0,1,5,42,0,0,1,9,240];
|
let bytes = [0,0,0,1,9,240,0,0,1,7,1,0,0,1,5,42,0,0,1,9,240];
|
||||||
assert_eq!(aud_offsets(&bytes), vec![0,16]);
|
assert_eq!(aud_offsets(&bytes), vec![0,16]);
|
||||||
|
|||||||
@@ -0,0 +1,181 @@
|
|||||||
|
//! Scoped PulseAudio / PipeWire-pulse routing. Never inspect or log audio payloads.
|
||||||
|
//! `pactl` is used when present; plain PipeWire installs without it (default Ubuntu desktop)
|
||||||
|
//! use `pw-cli`/`pw-dump`/`wpctl`, where WirePlumber moves default-following streams itself.
|
||||||
|
use std::process::{Command, Stdio};
|
||||||
|
use std::sync::{Mutex, OnceLock};
|
||||||
|
use std::os::fd::AsRawFd;
|
||||||
|
const SINK: &str = "uu-remote-sink";
|
||||||
|
const MONITOR: &str = "uu-remote-sink.monitor";
|
||||||
|
#[derive(serde::Serialize, serde::Deserialize)]
|
||||||
|
struct Route { previous: String, module: Option<u32>, #[serde(default)] pipewire: bool }
|
||||||
|
struct State { route: Option<Route>, _lock: std::fs::File }
|
||||||
|
static STATE: OnceLock<Mutex<Option<State>>> = OnceLock::new();
|
||||||
|
fn path() -> std::path::PathBuf { crate::config::path().with_file_name("virtual_audio.json") }
|
||||||
|
fn pactl(args: &[&str]) -> Result<String, ()> { run("pactl", args) }
|
||||||
|
fn run(program: &str, args: &[&str]) -> Result<String, ()> {
|
||||||
|
let out = Command::new(program).args(args).env("LC_ALL","C").stdin(Stdio::null()).stderr(Stdio::null()).output().map_err(|_| ())?;
|
||||||
|
if !out.status.success() { return Err(()); }
|
||||||
|
String::from_utf8(out.stdout).map(|s|s.trim().to_owned()).map_err(|_| ())
|
||||||
|
}
|
||||||
|
fn pipewire_only() -> bool { pactl(&["info"]).is_err() && run("wpctl",&["status"]).is_ok() }
|
||||||
|
/// Node ids whose node.name is `name`; `null_sink` restricts to null-audio-sink nodes (ownership).
|
||||||
|
fn pw_nodes(dump: &str, name: &str, null_sink: bool) -> Vec<u32> {
|
||||||
|
let Ok(serde_json::Value::Array(objects)) = serde_json::from_str(dump) else { return Vec::new() };
|
||||||
|
objects.iter().filter_map(|o| {
|
||||||
|
let props = o.get("info")?.get("props")?;
|
||||||
|
if props.get("node.name")?.as_str()? != name { return None; }
|
||||||
|
if null_sink && props.get("factory.name").and_then(|v| v.as_str()) != Some("support.null-audio-sink") { return None; }
|
||||||
|
u32::try_from(o.get("id")?.as_u64()?).ok()
|
||||||
|
}).collect()
|
||||||
|
}
|
||||||
|
fn pw_node(name: &str, null_sink: bool) -> Option<u32> { pw_nodes(&run("pw-dump",&["Node"]).ok()?, name, null_sink).first().copied() }
|
||||||
|
fn default_sink(pipewire: bool) -> Result<String, ()> {
|
||||||
|
if !pipewire { return pactl(&["get-default-sink"]); }
|
||||||
|
run("wpctl",&["inspect","@DEFAULT_AUDIO_SINK@"])?.lines()
|
||||||
|
.find_map(|l| l.trim().trim_start_matches("* ").strip_prefix("node.name = ").map(|v| v.trim_matches('"').to_owned())).ok_or(())
|
||||||
|
}
|
||||||
|
fn set_default(pipewire: bool, name: &str) -> Result<(), ()> {
|
||||||
|
if !pipewire { return pactl(&["set-default-sink",name]).map(|_| ()); }
|
||||||
|
run("wpctl",&["set-default",&pw_node(name,false).ok_or(())?.to_string()]).map(|_| ())
|
||||||
|
}
|
||||||
|
fn owned_modules(list: &str) -> Vec<u32> {
|
||||||
|
list.lines().filter_map(|line| {
|
||||||
|
let mut fields = line.split('\t');
|
||||||
|
let id = fields.next()?.parse().ok()?;
|
||||||
|
if fields.next()? != "module-null-sink" { return None; }
|
||||||
|
fields.next()?.split_whitespace().any(|arg| arg == "sink_name=uu-remote-sink").then_some(id)
|
||||||
|
}).collect()
|
||||||
|
}
|
||||||
|
fn sink_id(name: &str) -> Option<String> {
|
||||||
|
pactl(&["list","short","sinks"]).ok()?.lines().find_map(|line| {
|
||||||
|
let mut f=line.split_whitespace(); let id=f.next()?; (f.next()? == name).then(|| id.to_owned())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
fn move_inputs(from: &str, to: &str) {
|
||||||
|
let Some(id)=sink_id(from) else { return };
|
||||||
|
if let Ok(list)=pactl(&["list","short","sink-inputs"]) {
|
||||||
|
for line in list.lines() {
|
||||||
|
let mut f=line.split_whitespace();
|
||||||
|
if let (Some(input),Some(sink))=(f.next(),f.next()) {
|
||||||
|
if sink==id { let _=pactl(&["move-sink-input",input,to]); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn persist(route: &Route) -> Result<(), ()> {
|
||||||
|
use std::os::unix::fs::OpenOptionsExt;
|
||||||
|
use std::io::Write;
|
||||||
|
let temp=path().with_extension("json.new");
|
||||||
|
let mut file=std::fs::OpenOptions::new().write(true).create(true).truncate(true).mode(0o600).open(&temp).map_err(|_| ())?;
|
||||||
|
file.write_all(&serde_json::to_vec(route).map_err(|_| ())?).map_err(|_| ())?;
|
||||||
|
file.sync_all().map_err(|_| ())?;
|
||||||
|
std::fs::rename(temp,path()).map_err(|_| ())
|
||||||
|
}
|
||||||
|
fn recover(route: &Route) -> Result<(), ()> {
|
||||||
|
// Do not overwrite a default explicitly changed by the user during remote control.
|
||||||
|
// The previous output may be gone (unplugged USB card): still unload our sink so the server
|
||||||
|
// picks another default instead of leaving every stream on a null sink.
|
||||||
|
let pw=route.pipewire;
|
||||||
|
if default_sink(pw).is_ok_and(|s| s == SINK) && set_default(pw,&route.previous).is_err() {
|
||||||
|
log::warn!("Previous default audio output unavailable; the sound server will choose a new default");
|
||||||
|
}
|
||||||
|
if pw {
|
||||||
|
for id in pw_nodes(&run("pw-dump",&["Node"])?,SINK,true) { run("pw-cli",&["destroy",&id.to_string()])?; }
|
||||||
|
} else {
|
||||||
|
move_inputs(SINK,&route.previous);
|
||||||
|
let modules=owned_modules(&pactl(&["list","short","modules"])?);
|
||||||
|
for id in modules { pactl(&["unload-module",&id.to_string()])?; }
|
||||||
|
}
|
||||||
|
let _=std::fs::remove_file(path());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
fn state() -> &'static Mutex<Option<State>> {
|
||||||
|
STATE.get_or_init(|| {
|
||||||
|
use std::os::unix::fs::OpenOptionsExt;
|
||||||
|
let init=(|| {
|
||||||
|
std::fs::create_dir_all(path().parent().ok_or(())?).map_err(|_| ())?;
|
||||||
|
let lock=std::fs::OpenOptions::new().read(true).write(true).create(true).truncate(false).mode(0o600)
|
||||||
|
.open(path().with_extension("lock")).map_err(|_| ())?;
|
||||||
|
if unsafe { libc::flock(lock.as_raw_fd(),libc::LOCK_EX|libc::LOCK_NB) } != 0 { return Err(()); }
|
||||||
|
let route=if path().exists() {
|
||||||
|
let old:Route=serde_json::from_slice(&std::fs::read(path()).map_err(|_| ())?).map_err(|_| ())?;
|
||||||
|
if recover(&old).is_err() { Some(old) } else { None }
|
||||||
|
} else { None };
|
||||||
|
Ok(State { route, _lock:lock })
|
||||||
|
})();
|
||||||
|
Mutex::new(init.ok())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/// Called on startup even if mute is disabled, to recover a previous crash.
|
||||||
|
pub fn init() { let _=state(); }
|
||||||
|
pub fn enable() -> bool {
|
||||||
|
let mut guard=state().lock().unwrap();
|
||||||
|
let Some(state)=guard.as_mut() else { return false };
|
||||||
|
if state.route.is_some() { return pactl(&["get-default-sink"]).is_ok_and(|s|s==SINK); }
|
||||||
|
let result=(|| {
|
||||||
|
let pipewire=pipewire_only();
|
||||||
|
let previous=default_sink(pipewire)?;
|
||||||
|
if previous==SINK { return Err(()); }
|
||||||
|
// Refuse to claim another instance's or manually-created sink.
|
||||||
|
if if pipewire { pw_node(SINK,false).is_some() } else { sink_id(SINK).is_some() } { return Err(()); }
|
||||||
|
let mut route=Route { previous, module:None, pipewire };
|
||||||
|
persist(&route)?; // Save before loading: also covers a crash between load and ID persistence.
|
||||||
|
state.route=Some(Route { previous:route.previous.clone(), module:None, pipewire });
|
||||||
|
let id=if pipewire {
|
||||||
|
// object.linger keeps the node after pw-cli exits; recover() destroys it.
|
||||||
|
run("pw-cli",&["create-node","adapter","{ factory.name=support.null-audio-sink node.name=uu-remote-sink \
|
||||||
|
node.description=UU-Remote media.class=Audio/Sink object.linger=true audio.position=[FL FR] }"])?;
|
||||||
|
let mut node=None;
|
||||||
|
for _ in 0..20 { node=pw_node(SINK,true); if node.is_some() { break; } std::thread::sleep(std::time::Duration::from_millis(50)); }
|
||||||
|
node.ok_or(())?
|
||||||
|
} else {
|
||||||
|
pactl(&["load-module","module-null-sink","sink_name=uu-remote-sink","sink_properties=device.description=UU-Remote"])?.parse().map_err(|_| ())?
|
||||||
|
};
|
||||||
|
route.module=Some(id);
|
||||||
|
persist(&route)?;
|
||||||
|
state.route=Some(route);
|
||||||
|
set_default(pipewire,SINK)?;
|
||||||
|
if !pipewire { move_inputs(&state.route.as_ref().ok_or(())?.previous,SINK); }
|
||||||
|
Ok(())
|
||||||
|
})();
|
||||||
|
if result.is_err() {
|
||||||
|
if let Some(route)=state.route.as_ref() { if recover(route).is_ok() { state.route=None; } }
|
||||||
|
log::warn!("Virtual audio routing unavailable; using the existing mute/monitor path");
|
||||||
|
}
|
||||||
|
result.is_ok()
|
||||||
|
}
|
||||||
|
pub fn disable() {
|
||||||
|
let mut guard=state().lock().unwrap();
|
||||||
|
if let Some(state)=guard.as_mut() {
|
||||||
|
if let Some(route)=state.route.as_ref() {
|
||||||
|
if recover(route).is_ok() { state.route=None; }
|
||||||
|
else { log::warn!("Virtual audio restore incomplete; recovery record retained"); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn active() -> bool { state().lock().unwrap().as_ref().is_some_and(|s|s.route.is_some()) }
|
||||||
|
pub fn source() -> String {
|
||||||
|
if state().lock().unwrap().as_ref().is_some_and(|s|s.route.as_ref().is_some_and(|r|r.module.is_some())) { MONITOR.into() }
|
||||||
|
else { std::env::var("UU_AUDIO_SOURCE").unwrap_or_else(|_| "@DEFAULT_MONITOR@".into()) }
|
||||||
|
}
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test] fn recovery_only_claims_exact_null_sink() {
|
||||||
|
let modules="1\tmodule-null-sink\tsink_name=uu-remote-sink\t0\n2\tmodule-null-sink\tsink_name=uu-remote-sink-other\t0\n3\tmodule-other\tsink_name=uu-remote-sink\t0\n";
|
||||||
|
assert_eq!(owned_modules(modules),vec![1]);
|
||||||
|
}
|
||||||
|
#[test] fn pipewire_recovery_only_claims_owned_null_sink() {
|
||||||
|
let dump=r#"[{"id":5,"info":{"props":{"node.name":"uu-remote-sink","factory.name":"support.null-audio-sink"}}},
|
||||||
|
{"id":6,"info":{"props":{"node.name":"uu-remote-sink","factory.name":"api.alsa.pcm.sink"}}},
|
||||||
|
{"id":7,"info":{"props":{"node.name":"other","factory.name":"support.null-audio-sink"}}},{"id":8}]"#;
|
||||||
|
assert_eq!(pw_nodes(dump,"uu-remote-sink",true),vec![5]);
|
||||||
|
assert_eq!(pw_nodes(dump,"uu-remote-sink",false),vec![5,6]);
|
||||||
|
let old:Route=serde_json::from_str(r#"{"previous":"a","module":3}"#).unwrap();
|
||||||
|
assert!(!old.pipewire);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Also restore on ordinary returns from the host; SIGKILL is recovered next startup.
|
||||||
|
pub struct Cleanup;
|
||||||
|
impl Drop for Cleanup { fn drop(&mut self) { disable(); } }
|
||||||
+27
-1
@@ -7,6 +7,13 @@ extern "C" {
|
|||||||
fn uu_wl_button(button: i32, pressed: i32) -> i32;
|
fn uu_wl_button(button: i32, pressed: i32) -> i32;
|
||||||
fn uu_wl_key(symbol: u64, pressed: i32) -> i32;
|
fn uu_wl_key(symbol: u64, pressed: i32) -> i32;
|
||||||
fn uu_wl_wake() -> i32;
|
fn uu_wl_wake() -> i32;
|
||||||
|
fn uu_wl_release_all() -> i32;
|
||||||
|
}
|
||||||
|
pub fn release_all()->i32 {
|
||||||
|
#[cfg(all(target_os="linux",feature="wayland"))]
|
||||||
|
{return unsafe {uu_wl_release_all()};}
|
||||||
|
#[cfg(not(all(target_os="linux",feature="wayland")))]
|
||||||
|
-1
|
||||||
}
|
}
|
||||||
pub fn init() -> Result<(u32,u32),String> {
|
pub fn init() -> Result<(u32,u32),String> {
|
||||||
#[cfg(all(target_os="linux", feature="wayland"))]
|
#[cfg(all(target_os="linux", feature="wayland"))]
|
||||||
@@ -58,7 +65,8 @@ pub fn capture_stdout() -> Result<(),String> {
|
|||||||
let (w,h)=init()?;
|
let (w,h)=init()?;
|
||||||
let mut frame=vec![0u8;(w as usize)*(h as usize)*4];
|
let mut frame=vec![0u8;(w as usize)*(h as usize)*4];
|
||||||
let mut stdout=std::io::stdout().lock();
|
let mut stdout=std::io::stdout().lock();
|
||||||
let interval=std::time::Duration::from_nanos(1_000_000_000/crate::video::FPS);
|
let fps=capture_fps(std::env::var("UU_WAYLAND_FPS").ok().as_deref())?;
|
||||||
|
let interval=std::time::Duration::from_nanos(1_000_000_000/u64::from(fps));
|
||||||
loop {
|
loop {
|
||||||
let start=std::time::Instant::now();
|
let start=std::time::Instant::now();
|
||||||
capture(&mut frame)?;
|
capture(&mut frame)?;
|
||||||
@@ -67,6 +75,24 @@ pub fn capture_stdout() -> Result<(),String> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn capture_fps(value:Option<&str>)->Result<u32,String> {
|
||||||
|
match value {
|
||||||
|
None=>Ok(crate::video::FPS as u32),
|
||||||
|
Some(value)=>value.parse::<u32>().ok().filter(|fps|(5..=144).contains(fps))
|
||||||
|
.ok_or_else(||"UU_WAYLAND_FPS must be between 5 and 144".to_owned()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn capture_rate_accepts_viewer_range_and_rejects_invalid_values() {
|
||||||
|
assert_eq!(super::capture_fps(None),Ok(60));
|
||||||
|
for fps in [5,15,30,60,90,144] {assert_eq!(super::capture_fps(Some(&fps.to_string())),Ok(fps));}
|
||||||
|
for value in ["0","4","145","-1","NaN",""] {assert!(super::capture_fps(Some(value)).is_err());}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Use only in an isolated acceptance desktop; emits fixed test events, no logs.
|
/// Use only in an isolated acceptance desktop; emits fixed test events, no logs.
|
||||||
pub fn input_probe()->Result<(),String> {
|
pub fn input_probe()->Result<(),String> {
|
||||||
let (w,h)=init()?;
|
let (w,h)=init()?;
|
||||||
|
|||||||
+85
-8
@@ -86,6 +86,7 @@ impl WebRtcSession {
|
|||||||
.map_err(|e| format!("Create PeerConnection failed: {}", e))?,
|
.map_err(|e| format!("Create PeerConnection failed: {}", e))?,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let input_session=desktop.then(||input.desktop_session());
|
||||||
let context = Arc::new(Mutex::new(SessionContext::default()));
|
let context = Arc::new(Mutex::new(SessionContext::default()));
|
||||||
crate::network_diagnostics::start(Arc::downgrade(&pc));
|
crate::network_diagnostics::start(Arc::downgrade(&pc));
|
||||||
|
|
||||||
@@ -143,7 +144,7 @@ impl WebRtcSession {
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
let connected_context = context.clone();
|
let connected_context = context.clone();
|
||||||
let connection_input=input.clone();
|
let connection_input=input_session.clone();
|
||||||
let connected_signal = signaling_client.clone();
|
let connected_signal = signaling_client.clone();
|
||||||
let notified = Arc::new(std::sync::atomic::AtomicBool::new(false));
|
let notified = Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||||
pc.on_peer_connection_state_change(Box::new(move |state| {
|
pc.on_peer_connection_state_change(Box::new(move |state| {
|
||||||
@@ -153,7 +154,10 @@ impl WebRtcSession {
|
|||||||
let notified = notified.clone();
|
let notified = notified.clone();
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
log::info!("PeerConnection State changed: {:?}", state);
|
log::info!("PeerConnection State changed: {:?}", state);
|
||||||
if matches!(state,RTCPeerConnectionState::Closed|RTCPeerConnectionState::Failed|RTCPeerConnectionState::Disconnected) {input.release_held();}
|
if let Some(session)=input.as_ref() {
|
||||||
|
if state==RTCPeerConnectionState::Connected {session.connected();}
|
||||||
|
if matches!(state,RTCPeerConnectionState::Closed|RTCPeerConnectionState::Failed|RTCPeerConnectionState::Disconnected) {session.disconnected();}
|
||||||
|
}
|
||||||
if state == RTCPeerConnectionState::Connected && !notified.swap(true, std::sync::atomic::Ordering::AcqRel) {
|
if state == RTCPeerConnectionState::Connected && !notified.swap(true, std::sync::atomic::Ordering::AcqRel) {
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let ctx = ctx.lock().await.clone();
|
let ctx = ctx.lock().await.clone();
|
||||||
@@ -184,8 +188,11 @@ impl WebRtcSession {
|
|||||||
"video_0".to_owned(),
|
"video_0".to_owned(),
|
||||||
));
|
));
|
||||||
|
|
||||||
|
// Set by PLI/FIR; the desktop encoder answers with an IDR on its next frame.
|
||||||
|
let idr_request = Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||||
let sender = pc.add_track(video_track.clone()).await
|
let sender = pc.add_track(video_track.clone()).await
|
||||||
.map_err(|e| format!("Add video track failed: {}", e))?;
|
.map_err(|e| format!("Add video track failed: {}", e))?;
|
||||||
|
let idr_on_feedback = idr_request.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let mut feedback = [0u8; 1500];
|
let mut feedback = [0u8; 1500];
|
||||||
let mut reports = 0usize;
|
let mut reports = 0usize;
|
||||||
@@ -193,6 +200,14 @@ impl WebRtcSession {
|
|||||||
while let Ok((packets, _)) = sender.read(&mut feedback).await {
|
while let Ok((packets, _)) = sender.read(&mut feedback).await {
|
||||||
for packet in packets {
|
for packet in packets {
|
||||||
feedback_counts.observe(packet.as_any());
|
feedback_counts.observe(packet.as_any());
|
||||||
|
if packet.as_any().is::<webrtc::rtcp::payload_feedbacks::picture_loss_indication::PictureLossIndication>()
|
||||||
|
|| packet.as_any().is::<webrtc::rtcp::payload_feedbacks::full_intra_request::FullIntraRequest>() {
|
||||||
|
// Dedupe only while an IDR is pending; the encoder clears the flag when it emits one,
|
||||||
|
// so a lost IDR does not block the next recovery request.
|
||||||
|
if !idr_on_feedback.load(std::sync::atomic::Ordering::Acquire) {
|
||||||
|
idr_on_feedback.store(true, std::sync::atomic::Ordering::Release);
|
||||||
|
}
|
||||||
|
}
|
||||||
if let Some(report) = packet.as_any().downcast_ref::<webrtc::rtcp::receiver_report::ReceiverReport>() {
|
if let Some(report) = packet.as_any().downcast_ref::<webrtc::rtcp::receiver_report::ReceiverReport>() {
|
||||||
reports += 1;
|
reports += 1;
|
||||||
if reports == 1 || reports % 10 == 0 {
|
if reports == 1 || reports % 10 == 0 {
|
||||||
@@ -261,7 +276,7 @@ impl WebRtcSession {
|
|||||||
// File/terminal/port-mapping connections carry no desktop video.
|
// File/terminal/port-mapping connections carry no desktop video.
|
||||||
if desktop {
|
if desktop {
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
if let Err(e) = crate::video::stream_desktop(streaming_pc, video_track, capture_size, capture_display, portal, video_settings_rx).await {
|
if let Err(e) = crate::video::stream_desktop(streaming_pc, video_track, capture_size, capture_display, portal, video_settings_rx, idr_request).await {
|
||||||
log::error!("Live desktop stream stopped: {}", e);
|
log::error!("Live desktop stream stopped: {}", e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -282,6 +297,18 @@ impl WebRtcSession {
|
|||||||
let file_channel: Arc<std::sync::Mutex<Option<Arc<RTCDataChannel>>>> = Arc::new(std::sync::Mutex::new(None));
|
let file_channel: Arc<std::sync::Mutex<Option<Arc<RTCDataChannel>>>> = Arc::new(std::sync::Mutex::new(None));
|
||||||
let (file_tx, mut file_rx) = tokio::sync::mpsc::unbounded_channel::<Vec<u8>>();
|
let (file_tx, mut file_rx) = tokio::sync::mpsc::unbounded_channel::<Vec<u8>>();
|
||||||
let peer_files = crate::file_trans::FileSession::new(file_tx, control_sequence.clone());
|
let peer_files = crate::file_trans::FileSession::new(file_tx, control_sequence.clone());
|
||||||
|
// 端口映射 frames (Message 27) also travel on FILE_DATA_CHANNEL; one session per peer.
|
||||||
|
let port_mapping = crate::port_mapping::Session::new(file_channel.clone(), control_sequence.clone());
|
||||||
|
{
|
||||||
|
let (end_pc, mapping) = (pc.clone(), port_mapping.clone());
|
||||||
|
tokio::spawn(async move {
|
||||||
|
use webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState as State;
|
||||||
|
while !matches!(end_pc.connection_state(), State::Closed | State::Failed) {
|
||||||
|
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||||
|
}
|
||||||
|
mapping.shutdown();
|
||||||
|
});
|
||||||
|
}
|
||||||
{
|
{
|
||||||
let slot = file_channel.clone();
|
let slot = file_channel.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
@@ -304,10 +331,12 @@ impl WebRtcSession {
|
|||||||
log::info!("New DataChannel received: {}", label);
|
log::info!("New DataChannel received: {}", label);
|
||||||
|
|
||||||
let input_inst = input_clone.clone();
|
let input_inst = input_clone.clone();
|
||||||
|
let channel_input_session=input_session.clone();
|
||||||
let screen = control_capture.clone();
|
let screen = control_capture.clone();
|
||||||
let out_sequence = control_sequence.clone();
|
let out_sequence = control_sequence.clone();
|
||||||
let shared_clipboard_ready = text_clipboard_ready.clone();
|
let shared_clipboard_ready = text_clipboard_ready.clone();
|
||||||
let peer_files = peer_files.clone();
|
let peer_files = peer_files.clone();
|
||||||
|
let peer_port_mapping = port_mapping.clone();
|
||||||
let peer_video_settings = video_settings.clone();
|
let peer_video_settings = video_settings.clone();
|
||||||
let peer_actions = peer_actions.clone();
|
let peer_actions = peer_actions.clone();
|
||||||
if label == "FILE_DATA_CHANNEL" { *file_channel.lock().unwrap() = Some(d.clone()); }
|
if label == "FILE_DATA_CHANNEL" { *file_channel.lock().unwrap() = Some(d.clone()); }
|
||||||
@@ -324,6 +353,13 @@ impl WebRtcSession {
|
|||||||
let clipboard_assembly = Arc::new(std::sync::Mutex::new(crate::clipboard_protocol::Assembly::default()));
|
let clipboard_assembly = Arc::new(std::sync::Mutex::new(crate::clipboard_protocol::Assembly::default()));
|
||||||
let rich_state=Arc::new(std::sync::Mutex::new(crate::clipboard_rich::State::default()));
|
let rich_state=Arc::new(std::sync::Mutex::new(crate::clipboard_rich::State::default()));
|
||||||
let clipboard_enabled = shared_clipboard_ready.clone();
|
let clipboard_enabled = shared_clipboard_ready.clone();
|
||||||
|
if label=="CONTROL_DATA_CHANNEL" {
|
||||||
|
let closing=channel_input_session.clone();
|
||||||
|
d.on_close(Box::new(move || {
|
||||||
|
let closing=closing.clone();
|
||||||
|
Box::pin(async move {if let Some(session)=closing {session.disconnected();}})
|
||||||
|
}));
|
||||||
|
}
|
||||||
// Ordered outbound queue for protocol replies produced outside this callback.
|
// Ordered outbound queue for protocol replies produced outside this callback.
|
||||||
let (outbound, mut outbound_rx) = tokio::sync::mpsc::unbounded_channel::<Vec<u8>>();
|
let (outbound, mut outbound_rx) = tokio::sync::mpsc::unbounded_channel::<Vec<u8>>();
|
||||||
{
|
{
|
||||||
@@ -414,18 +450,22 @@ impl WebRtcSession {
|
|||||||
let next_seq = || sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
let next_seq = || sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||||
// Default file-transfer folder: the desktop user's home directory.
|
// Default file-transfer folder: the desktop user's home directory.
|
||||||
let home = std::env::var("HOME").unwrap_or_else(|_| "/".into());
|
let home = std::env::var("HOME").unwrap_or_else(|_| "/".into());
|
||||||
let desktop_permission=if screen.init_error.is_none() {
|
// Tool peers are permitted without a desktop; the port-mapping viewer aborts on Reject.
|
||||||
|
let desktop_permission=if screen.init_error.is_none() || !desktop {
|
||||||
crate::system_metrics::Permission::Allow
|
crate::system_metrics::Permission::Allow
|
||||||
} else { crate::system_metrics::Permission::Reject };
|
} else { crate::system_metrics::Permission::Reject };
|
||||||
|
// Without a desktop (headless/portal before consent) the port-mapping tool still waits
|
||||||
|
// for ScreenSources/CaptureChange and aborts otherwise: tool peers get a nominal screen.
|
||||||
|
let screen_size=if screen.init_error.is_none() { (screen.width, screen.height) } else { (1920, 1080) };
|
||||||
let messages = [
|
let messages = [
|
||||||
(8, crate::initialization::encode_capture_change(next_seq(), timestamp, 0, 0, "Linux desktop")),
|
(8, crate::initialization::encode_capture_change(next_seq(), timestamp, 0, 0, "Linux desktop")),
|
||||||
(13, Ok(crate::initialization::envelope(next_seq(), timestamp, 13, &init_actions.metrics_payload(desktop_sound, desktop_permission)))),
|
(13, Ok(crate::initialization::envelope(next_seq(), timestamp, 13, &init_actions.metrics_payload(desktop_sound, desktop_permission)))),
|
||||||
(26, Ok(crate::initialization::envelope(next_seq(), timestamp, 26,
|
(26, Ok(crate::initialization::envelope(next_seq(), timestamp, 26,
|
||||||
&crate::system_metrics::remote_download_path(&home)))),
|
&crate::system_metrics::remote_download_path(&home)))),
|
||||||
(7, crate::capture_setting::screen_sources(next_seq(), timestamp, (screen.width, screen.height), init_video.borrow().output, "Linux desktop")),
|
(7, crate::capture_setting::screen_sources(next_seq(), timestamp, screen_size, init_video.borrow().output, "Linux desktop")),
|
||||||
];
|
];
|
||||||
for (field, message) in messages {
|
for (field, message) in messages {
|
||||||
if screen.init_error.is_some() && matches!(field,7|8) { continue; }
|
if desktop && screen.init_error.is_some() && matches!(field,7|8) { continue; }
|
||||||
let message = match message {
|
let message = match message {
|
||||||
Ok(message) => message,
|
Ok(message) => message,
|
||||||
Err(e) => { log::error!("CONTROL initialization encode failed: {}", e); return; }
|
Err(e) => { log::error!("CONTROL initialization encode failed: {}", e); return; }
|
||||||
@@ -470,6 +510,7 @@ impl WebRtcSession {
|
|||||||
d.on_message(Box::new(move |msg: DataChannelMessage| {
|
d.on_message(Box::new(move |msg: DataChannelMessage| {
|
||||||
let channel_name = lbl.clone();
|
let channel_name = lbl.clone();
|
||||||
let input_worker = inp.clone();
|
let input_worker = inp.clone();
|
||||||
|
let message_input_session=channel_input_session.clone();
|
||||||
let terminal_worker = terminal_conn.clone();
|
let terminal_worker = terminal_conn.clone();
|
||||||
let rpc_out = outbound.clone();
|
let rpc_out = outbound.clone();
|
||||||
let reply_chan = d_reply.clone();
|
let reply_chan = d_reply.clone();
|
||||||
@@ -479,6 +520,7 @@ impl WebRtcSession {
|
|||||||
let clipboard_ready = clipboard_enabled.clone();
|
let clipboard_ready = clipboard_enabled.clone();
|
||||||
let rich_incoming=rich_state.clone();
|
let rich_incoming=rich_state.clone();
|
||||||
let file_worker = file_session.clone();
|
let file_worker = file_session.clone();
|
||||||
|
let mapping = peer_port_mapping.clone();
|
||||||
let video_settings = channel_video_settings.clone();
|
let video_settings = channel_video_settings.clone();
|
||||||
let actions = channel_actions.clone();
|
let actions = channel_actions.clone();
|
||||||
|
|
||||||
@@ -517,6 +559,9 @@ impl WebRtcSession {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if let Ok(fields) = crate::protocol::decode_wire(&msg.data) {
|
||||||
|
if mapping.handle(&fields, &channel_name) { return; }
|
||||||
|
}
|
||||||
if let Some(files) = &file_worker {
|
if let Some(files) = &file_worker {
|
||||||
if let Ok(fields) = crate::protocol::decode_wire(&msg.data) {
|
if let Ok(fields) = crate::protocol::decode_wire(&msg.data) {
|
||||||
if files.handle(&fields) {
|
if files.handle(&fields) {
|
||||||
@@ -728,6 +773,7 @@ impl WebRtcSession {
|
|||||||
if RECEIPTS[slot].fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 16 {
|
if RECEIPTS[slot].fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 16 {
|
||||||
log::info!("CONTROL input received: kind={}", kind);
|
log::info!("CONTROL input received: kind={}", kind);
|
||||||
}
|
}
|
||||||
|
if let Some(session)=message_input_session.as_ref() {session.with_input(|| {
|
||||||
match &event {
|
match &event {
|
||||||
ControlInput::MouseAbsolute {x,y} => input_worker.mouse_absolute_normalized(*x,*y,input_size),
|
ControlInput::MouseAbsolute {x,y} => input_worker.mouse_absolute_normalized(*x,*y,input_size),
|
||||||
ControlInput::MouseRelative {dx,dy} => input_worker.mouse_relative(*dx,*dy),
|
ControlInput::MouseRelative {dx,dy} => input_worker.mouse_relative(*dx,*dy),
|
||||||
@@ -738,6 +784,7 @@ impl WebRtcSession {
|
|||||||
ControlInput::MouseClick {button} => {input_worker.wire_mouse_button(*button,true);input_worker.wire_mouse_button(*button,false);},
|
ControlInput::MouseClick {button} => {input_worker.wire_mouse_button(*button,true);input_worker.wire_mouse_button(*button,false);},
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
});}
|
||||||
|
|
||||||
},
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
@@ -792,7 +839,7 @@ impl WebRtcSession {
|
|||||||
ctx.client_id = soac.client_id.clone();
|
ctx.client_id = soac.client_id.clone();
|
||||||
ctx.ice_id = soac.ice_id.clone();
|
ctx.ice_id = soac.ice_id.clone();
|
||||||
ctx.app_control_id = soac.app_control_id.clone();
|
ctx.app_control_id = soac.app_control_id.clone();
|
||||||
log::info!("Updated WebRTC session context: client_id={:?}, ice_id={:?}", ctx.client_id, ctx.ice_id);
|
log::info!("Updated WebRTC session context: client_id_present={} ice_id_present={}", ctx.client_id.is_some(), ctx.ice_id.is_some());
|
||||||
}
|
}
|
||||||
let mut sdp_str = soac.sdp.clone().unwrap_or_default();
|
let mut sdp_str = soac.sdp.clone().unwrap_or_default();
|
||||||
if sdp_str.is_empty() {
|
if sdp_str.is_empty() {
|
||||||
@@ -841,7 +888,11 @@ impl WebRtcSession {
|
|||||||
.set_local_description(answer.clone())
|
.set_local_description(answer.clone())
|
||||||
.await
|
.await
|
||||||
.map_err(|e| format!("Set local desc failed: {}", e))?;
|
.map_err(|e| format!("Set local desc failed: {}", e))?;
|
||||||
Ok(answer.sdp)
|
// webrtc-rs omits a=max-message-size, so the viewer (libwebrtc) assumes 64 KB and refuses
|
||||||
|
// larger data-channel messages (port-mapping uploads stalled after the first frame).
|
||||||
|
// Only the copy sent to the viewer is amended: webrtc-rs rejects a local description that
|
||||||
|
// differs from the answer it generated. Our receive buffer (1 MB) accepts 256 KB messages.
|
||||||
|
Ok(with_max_message_size(&answer.sdp))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 接收远端 ICE Candidate
|
/// 接收远端 ICE Candidate
|
||||||
@@ -882,6 +933,23 @@ impl WebRtcSession {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Max SCTP message size we accept (libwebrtc's default, as the official host advertises).
|
||||||
|
const MAX_MESSAGE_SIZE: usize = 262_144;
|
||||||
|
|
||||||
|
/// Adds `a=max-message-size` after `a=sctp-port` in the data-channel section if missing.
|
||||||
|
fn with_max_message_size(sdp: &str) -> String {
|
||||||
|
if sdp.contains("a=max-message-size:") { return sdp.to_owned(); }
|
||||||
|
let eol = if sdp.contains("\r\n") { "\r\n" } else { "\n" };
|
||||||
|
let mut out = String::with_capacity(sdp.len() + 32);
|
||||||
|
for line in sdp.split_inclusive('\n') {
|
||||||
|
out.push_str(line);
|
||||||
|
if line.trim_end().starts_with("a=sctp-port:") {
|
||||||
|
out.push_str(&format!("a=max-message-size:{MAX_MESSAGE_SIZE}{eol}"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
fn negotiated_bundle_media(sdp: &str) -> Option<(String, u16)> {
|
fn negotiated_bundle_media(sdp: &str) -> Option<(String, u16)> {
|
||||||
let preferred: Vec<&str> = sdp.lines().find_map(|l| l.strip_prefix("a=group:BUNDLE "))?
|
let preferred: Vec<&str> = sdp.lines().find_map(|l| l.strip_prefix("a=group:BUNDLE "))?
|
||||||
.split_whitespace().collect();
|
.split_whitespace().collect();
|
||||||
@@ -907,6 +975,15 @@ fn negotiated_bundle_media(sdp: &str) -> Option<(String, u16)> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::negotiated_bundle_media;
|
use super::negotiated_bundle_media;
|
||||||
|
#[test]
|
||||||
|
fn advertises_max_message_size_once_in_the_data_channel_section() {
|
||||||
|
use super::with_max_message_size;
|
||||||
|
let sdp = "v=0\r\nm=application 9 UDP/DTLS/SCTP webrtc-datachannel\r\na=sctp-port:5000\r\n";
|
||||||
|
let out = with_max_message_size(sdp);
|
||||||
|
assert!(out.contains("a=sctp-port:5000\r\na=max-message-size:262144\r\n"));
|
||||||
|
assert_eq!(with_max_message_size(&out), out);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn skips_rejected_audio_for_ice_transport() {
|
fn skips_rejected_audio_for_ice_transport() {
|
||||||
let sdp = "a=group:BUNDLE 1 2\r\nm=audio 0 RTP 0\r\na=mid:0\r\nm=video 9 RTP 98\r\na=mid:1\r\nm=application 9 SCTP 0\r\na=mid:2\r\n";
|
let sdp = "a=group:BUNDLE 1 2\r\nm=audio 0 RTP 0\r\na=mid:0\r\nm=video 9 RTP 98\r\na=mid:1\r\nm=application 9 SCTP 0\r\na=mid:2\r\n";
|
||||||
|
|||||||
+15
-1
@@ -6,6 +6,17 @@ final class MarkerCheck: NSObject {
|
|||||||
let kde = NSTextField(labelWithString: "KDE → Mac: not checked")
|
let kde = NSTextField(labelWithString: "KDE → Mac: not checked")
|
||||||
let copied = NSTextField(labelWithString: "Mac marker: not copied")
|
let copied = NSTextField(labelWithString: "Mac marker: not copied")
|
||||||
let formats = NSTextField(labelWithString: "Known text formats: not checked")
|
let formats = NSTextField(labelWithString: "Known text formats: not checked")
|
||||||
|
// The official viewer drops remote format offers before ACK unless its
|
||||||
|
// remote window is key (viewer 0x100380f20 -> isKeyWindow). A delayed
|
||||||
|
// marker-only comparison lets it stay key throughout negotiation.
|
||||||
|
@objc func armGnome() {
|
||||||
|
gnome.stringValue = "GNOME → Mac: armed; return to UU and copy VM marker"
|
||||||
|
DispatchQueue.main.asyncAfter(deadline: .now() + 15) { self.compareGnome() }
|
||||||
|
}
|
||||||
|
@objc func armKde() {
|
||||||
|
kde.stringValue = "KDE → Mac: armed; return to UU and copy VM marker"
|
||||||
|
DispatchQueue.main.asyncAfter(deadline: .now() + 15) { self.compareKde() }
|
||||||
|
}
|
||||||
@objc func copyMarker() {
|
@objc func copyMarker() {
|
||||||
NSPasteboard.general.clearContents()
|
NSPasteboard.general.clearContents()
|
||||||
let ok = NSPasteboard.general.setString("UU-Mac-to-VM 中文🙂", forType: .string)
|
let ok = NSPasteboard.general.setString("UU-Mac-to-VM 中文🙂", forType: .string)
|
||||||
@@ -32,14 +43,17 @@ final class MarkerCheck: NSObject {
|
|||||||
NSButton(title: title, target: self, action: action)
|
NSButton(title: title, target: self, action: action)
|
||||||
}
|
}
|
||||||
func show() {
|
func show() {
|
||||||
let window = NSWindow(contentRect: NSRect(x: 0,y: 0,width: 500,height: 360), styleMask: [.titled,.closable], backing: .buffered, defer: false)
|
let window = NSWindow(contentRect: NSRect(x: 0,y: 0,width: 620,height: 500), styleMask: [.titled,.closable], backing: .buffered, defer: false)
|
||||||
window.title = "UU synthetic clipboard acceptance"
|
window.title = "UU synthetic clipboard acceptance"
|
||||||
window.isReleasedWhenClosed = false
|
window.isReleasedWhenClosed = false
|
||||||
let stack = NSStackView(views: [
|
let stack = NSStackView(views: [
|
||||||
NSTextField(labelWithString: "Only fixed test-marker matches are displayed."),
|
NSTextField(labelWithString: "Only fixed test-marker matches are displayed."),
|
||||||
|
NSTextField(labelWithString: "Keep the UU remote window key until format ACK / text request."),
|
||||||
button("Copy Mac test marker", #selector(copyMarker)), copied,
|
button("Copy Mac test marker", #selector(copyMarker)), copied,
|
||||||
button("Compare GNOME VM marker", #selector(compareGnome)), gnome,
|
button("Compare GNOME VM marker", #selector(compareGnome)), gnome,
|
||||||
button("Compare KDE VM marker", #selector(compareKde)), kde, formats,
|
button("Compare KDE VM marker", #selector(compareKde)), kde, formats,
|
||||||
|
button("Arm GNOME comparison in 15 seconds", #selector(armGnome)),
|
||||||
|
button("Arm KDE comparison in 15 seconds", #selector(armKde)),
|
||||||
button("Quit test fixture", #selector(quit))])
|
button("Quit test fixture", #selector(quit))])
|
||||||
stack.orientation = .vertical; stack.spacing = 14
|
stack.orientation = .vertical; stack.spacing = 14
|
||||||
stack.translatesAutoresizingMaskIntoConstraints = false
|
stack.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
|||||||
Vendored
+7
-3
@@ -1,5 +1,5 @@
|
|||||||
"""Hold an ephemeral idle inhibition only in the isolated QEMU acceptance VM."""
|
"""Hold an ephemeral idle inhibition only in the isolated QEMU acceptance VM."""
|
||||||
import pathlib,subprocess,signal,threading,sys
|
import pathlib,subprocess,signal,threading,sys,os
|
||||||
assert pathlib.Path('/proc/sys/kernel/hostname').read_text().strip()=='uu-hyprland-test'
|
assert pathlib.Path('/proc/sys/kernel/hostname').read_text().strip()=='uu-hyprland-test'
|
||||||
assert subprocess.check_output(['systemd-detect-virt'],text=True).strip()=='qemu'
|
assert subprocess.check_output(['systemd-detect-virt'],text=True).strip()=='qemu'
|
||||||
import gi
|
import gi
|
||||||
@@ -7,7 +7,11 @@ gi.require_version('Gio','2.0')
|
|||||||
from gi.repository import Gio,GLib
|
from gi.repository import Gio,GLib
|
||||||
seconds=min(4200,max(1,int(sys.argv[1]) if len(sys.argv)>1 else 3600))
|
seconds=min(4200,max(1,int(sys.argv[1]) if len(sys.argv)>1 else 3600))
|
||||||
bus=Gio.bus_get_sync(Gio.BusType.SESSION,None)
|
bus=Gio.bus_get_sync(Gio.BusType.SESSION,None)
|
||||||
reply=bus.call_sync('org.freedesktop.ScreenSaver','/ScreenSaver','org.freedesktop.ScreenSaver','Inhibit',GLib.Variant('(ss)',('UU isolated acceptance','Timed live-connection acceptance')),GLib.VariantType.new('(u)'),Gio.DBusCallFlags.NONE,3000,None)
|
gnome='GNOME' in os.environ.get('XDG_CURRENT_DESKTOP','').upper().split(':')
|
||||||
|
destination='org.gnome.SessionManager' if gnome else 'org.freedesktop.ScreenSaver'
|
||||||
|
object_path='/org/gnome/SessionManager' if gnome else '/ScreenSaver'
|
||||||
|
parameters=GLib.Variant('(susu)',('UU isolated acceptance',0,'Timed live-connection acceptance',8)) if gnome else GLib.Variant('(ss)',('UU isolated acceptance','Timed live-connection acceptance'))
|
||||||
|
reply=bus.call_sync(destination,object_path,destination,'Inhibit',parameters,GLib.VariantType.new('(u)'),Gio.DBusCallFlags.NONE,3000,None)
|
||||||
cookie=reply.unpack()[0]
|
cookie=reply.unpack()[0]
|
||||||
stop=threading.Event()
|
stop=threading.Event()
|
||||||
signal.signal(signal.SIGTERM,lambda *_:stop.set())
|
signal.signal(signal.SIGTERM,lambda *_:stop.set())
|
||||||
@@ -16,6 +20,6 @@ print('VM timed idle inhibition acquired',flush=True)
|
|||||||
try:stop.wait(seconds)
|
try:stop.wait(seconds)
|
||||||
finally:
|
finally:
|
||||||
try:
|
try:
|
||||||
bus.call_sync('org.freedesktop.ScreenSaver','/ScreenSaver','org.freedesktop.ScreenSaver','UnInhibit',GLib.Variant('(u)',(cookie,)),None,Gio.DBusCallFlags.NONE,3000,None)
|
bus.call_sync(destination,object_path,destination,'Uninhibit' if gnome else 'UnInhibit',GLib.Variant('(u)',(cookie,)),None,Gio.DBusCallFlags.NONE,3000,None)
|
||||||
print('VM timed idle inhibition released',flush=True)
|
print('VM timed idle inhibition released',flush=True)
|
||||||
except GLib.Error:print('VM idle inhibition connection closed',flush=True)
|
except GLib.Error:print('VM idle inhibition connection closed',flush=True)
|
||||||
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
{"v":1}
|
||||||
Vendored
+6
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"git": {
|
||||||
|
"sha1": "502671d8023141b70051e85aaf8748e86ea64dfd"
|
||||||
|
},
|
||||||
|
"path_in_vcs": "webrtc"
|
||||||
|
}
|
||||||
Vendored
+236
@@ -0,0 +1,236 @@
|
|||||||
|
# webrtc-rs changelog
|
||||||
|
|
||||||
|
## Unreleased
|
||||||
|
|
||||||
|
## v0.7.0
|
||||||
|
|
||||||
|
* Added support for insecure/deprecated signature verification algorithms, opt in via `SettingsEngine::allow_insecure_verification_algorithm` [#342](https://github.com/webrtc-rs/webrtc/pull/342).
|
||||||
|
* Make RTCRtpCodecCapability::payloader_for_codec public API [#349](https://github.com/webrtc-rs/webrtc/pull/349).
|
||||||
|
* Fixed a panic in `calculate_rtt_ms` [#350](https://github.com/webrtc-rs/webrtc/pull/350).
|
||||||
|
* Fixed `TrackRemote` missing at least the first, sometimes more, RTP packet during probing. [#387](https://github.com/webrtc-rs/webrtc/pull/387)
|
||||||
|
|
||||||
|
### Breaking changes
|
||||||
|
|
||||||
|
* Change `RTCPeerConnection::on_track` callback signature to `|track: Arc<TrackRemote>, receiver: Arc<RTCRtpReceiver>, transceiver: Arc<RTCRtpTransceiver>|` [#355](https://github.com/webrtc-rs/webrtc/pull/355).
|
||||||
|
|
||||||
|
* Change `RTCRtpSender::new` signature to `|receive_mtu: usize, track: Option<Arc<dyn TrackLocal + Send + Sync>>, transport: Arc<RTCDtlsTransport>, media_engine: Arc<MediaEngine>, interceptor: Arc<dyn Interceptor + Send + Sync>, start_paused: bool,|` [#377](https://github.com/webrtc-rs/webrtc/pull/377).
|
||||||
|
|
||||||
|
* Change `API::new_rtp_sender` signature to `|&self, track: Option<Arc<dyn TrackLocal + Send + Sync>>, transport: Arc<RTCDtlsTransport>, interceptor: Arc<dyn Interceptor + Send + Sync>,|` [#377](https://github.com/webrtc-rs/webrtc/pull/377).
|
||||||
|
|
||||||
|
* Change `RTCRtpTransceiver::sender` signature to `|&self| -> Arc<RTCRtpSender>` [#377](https://github.com/webrtc-rs/webrtc/pull/377).
|
||||||
|
|
||||||
|
* Change `RTCRtpTransceiver::set_sender_track` signature to `|self: &Arc<Self>, sender: Arc<RTCRtpSender>, track: Option<Arc<dyn TrackLocal + Send + Sync>>,|` [#377](https://github.com/webrtc-rs/webrtc/pull/377).
|
||||||
|
|
||||||
|
* Change `RTCRtpTransceiver::set_sender` signature to `|self: &Arc<Self>, s: Arc<RTCRtpSender>|` [#377](https://github.com/webrtc-rs/webrtc/pull/377).
|
||||||
|
|
||||||
|
* Change `RTCRtpTransceiver::receiver` signature to `|&self| -> Arc<RTCRtpReceiver>` [#377](https://github.com/webrtc-rs/webrtc/pull/377).
|
||||||
|
|
||||||
|
* Change `RTCRtpTransceiver::set_receiver` signature to `|&self, r: Arc<RTCRtpReceiver>|` [#377](https://github.com/webrtc-rs/webrtc/pull/377).
|
||||||
|
|
||||||
|
* Change `RTCPeerConnection::add_transceiver_from_kind` signature to `|&self, kind: RTPCodecType, init: Option<RTCRtpTransceiverInit>,|`, `RTCRtpTransceiver::RTCRtpSender` сreated without a track [#377](https://github.com/webrtc-rs/webrtc/pull/377).
|
||||||
|
|
||||||
|
* Change `RTCPeerConnection::add_transceiver_from_track` signature to `|&self, track: Arc<dyn TrackLocal + Send + Sync>, init: Option<RTCRtpTransceiverInit>,|` [#377](https://github.com/webrtc-rs/webrtc/pull/377).
|
||||||
|
|
||||||
|
* Change `RTCPeerConnection::mid` return signature to `Option<String>` [#375](https://github.com/webrtc-rs/webrtc/pull/375).
|
||||||
|
|
||||||
|
* Make functions non-async [#402](https://github.com/webrtc-rs/webrtc/pull/402):
|
||||||
|
- `MediaEngine`:
|
||||||
|
- `get_codecs_by_kind`;
|
||||||
|
- `get_rtp_parameters_by_kind`.
|
||||||
|
- `RTCRtpTransceiver`:
|
||||||
|
- `sender`;
|
||||||
|
- `set_sender`;
|
||||||
|
- `receiver`.
|
||||||
|
- `RTPReceiverInternal`:
|
||||||
|
- `set_transceiver_codecs`;
|
||||||
|
- `get_codecs`.
|
||||||
|
- `RTCRtpSender`:
|
||||||
|
- `set_rtp_transceiver`;
|
||||||
|
- `has_sent`.
|
||||||
|
- `TrackRemote`:
|
||||||
|
- `id`;
|
||||||
|
- `set_id`;
|
||||||
|
- `stream_id`;
|
||||||
|
- `set_stream_id`;
|
||||||
|
- `msid`;
|
||||||
|
- `codec`;
|
||||||
|
- `set_codec`;
|
||||||
|
- `params`;
|
||||||
|
- `set_params`;
|
||||||
|
- `onmute`;
|
||||||
|
- `onunmute`.
|
||||||
|
|
||||||
|
* Change `RTPReader::read` signature to `|&self, buf: &mut [u8], attributes: &Attributes| -> Result<(rtp::packet::Packet, Attributes)>` [#450](https://github.com/webrtc-rs/webrtc/pull/450).
|
||||||
|
|
||||||
|
* Change `RTCPReader::read` signature to `|&self, buf: &mut [u8], attributes: &Attributes| -> Result<(Vec<Box<dyn rtcp::packet::Packet + Send + Sync>>, Attributes)>` [#450](https://github.com/webrtc-rs/webrtc/pull/450).
|
||||||
|
|
||||||
|
## v0.6.0
|
||||||
|
|
||||||
|
* Added more stats to `RemoteInboundRTPStats` and `RemoteOutboundRTPStats` [#282](https://github.com/webrtc-rs/webrtc/pull/282) by [@k0nserv](https://github.com/k0nserv).
|
||||||
|
* Don't register `video/rtx` codecs in `MediaEngine::register_default_codecs`. These weren't actually support and prevented RTX in the existing RTP stream from being used. Long term we should support RTX via this method, this is tracked in [#295](https://github.com/webrtc-rs/webrtc/issues/295). [#294 Remove video/rtx codecs](https://github.com/webrtc-rs/webrtc/pull/294) contributed by [k0nserv](https://github.com/k0nserv)
|
||||||
|
* Add IP filter to WebRTC `SettingEngine` [#306](https://github.com/webrtc-rs/webrtc/pull/306)
|
||||||
|
* Stop sequence numbers from increasing in `TrackLocalStaticSample` while the bound `RTCRtpSender` have
|
||||||
|
directions that should not send. [#316](https://github.com/webrtc-rs/webrtc/pull/316)
|
||||||
|
* Add support for a mime type "audio/telephone-event" (rfc4733) [#322](https://github.com/webrtc-rs/webrtc/pull/322)
|
||||||
|
* Fixed a panic that would sometimes happen when collecting stats. [#327](https://github.com/webrtc-rs/webrtc/pull/327) by [@k0nserv](https://github.com/k0nserv).
|
||||||
|
* Added new extension marshaller/unmarshaller for VideoOrientation, and made marshallers serializable via serde [#331](https://github.com/webrtc-rs/webrtc/pull/331) [#332](https://github.com/webrtc-rs/webrtc/pull/332)
|
||||||
|
* Updated minimum rust version to `1.60.0`
|
||||||
|
* Added a new `write_rtp_with_extensions` method to `TrackLocalStaticSample` and `TrackLocalStaticRTP`. [#336](https://github.com/webrtc-rs/webrtc/pull/336) by [@k0nserv](https://github.com/k0nserv).
|
||||||
|
* Added a new `sample_writer` helper to `TrackLocalStaticSample`. [#336](https://github.com/webrtc-rs/webrtc/pull/336) by [@k0nserv](https://github.com/k0nserv).
|
||||||
|
* Increased minimum versions for sub-dependencies:
|
||||||
|
* `webrtc-data` version to `0.6.0`.
|
||||||
|
* `webrtc-ice` version to `0.9.0`.
|
||||||
|
* `webrtc-media` version to `0.5.0`.
|
||||||
|
* `webrtc-sctp` version to `0.7.0`.
|
||||||
|
* `webrtc-util` version to `0.7.0`.
|
||||||
|
|
||||||
|
### Breaking changes
|
||||||
|
|
||||||
|
* Allowed one single direction for extmap matching. [#321](https://github.com/webrtc-rs/webrtc/pull/321). API change for `MediaEngine::register_header_extension`.
|
||||||
|
* Removed support for Plan-B. All major implementations of WebRTC now support unified and continuing support for plan-b is an undue maintenance burden when unified can be used. See [“Unified Plan” Transition Guide (JavaScript)](https://docs.google.com/document/d/1-ZfikoUtoJa9k-GZG1daN0BU3IjIanQ_JSscHxQesvU/) for an overview of the changes required to migrate. [#320](https://github.com/webrtc-rs/webrtc/pull/320) by [@algesten](https://github.com/algesten).
|
||||||
|
* Removed 2nd argument from `RTCCertificate::from_pem` and guard it with `pem` feature [#333]
|
||||||
|
* Renamed `RTCCertificate::pem` to `serialize_pem` and guard it with `pem` feature [#333]
|
||||||
|
* Removed `RTCCertificate::expires` [#333]
|
||||||
|
* `RTCCertificate::get_fingerprints` no longer returns `Result` [#333]
|
||||||
|
* Make functions non-async [#338](https://github.com/webrtc-rs/webrtc/pull/338):
|
||||||
|
- `RTCDataChannel`:
|
||||||
|
- `on_open`;
|
||||||
|
- `on_close`;
|
||||||
|
- `on_message`;
|
||||||
|
- `on_error`.
|
||||||
|
- `RTCDtlsTransport::on_state_change`;
|
||||||
|
- `RTCIceCandidate::to_json`;
|
||||||
|
- `RTCIceGatherer`:
|
||||||
|
- `on_local_candidate`;
|
||||||
|
- `on_state_change`;
|
||||||
|
- `on_gathering_complete`.
|
||||||
|
- `RTCIceTransport`:
|
||||||
|
- `get_selected_candidate_pair`;
|
||||||
|
- `on_selected_candidate_pair_change`;
|
||||||
|
- `on_connection_state_change`.
|
||||||
|
- `RTCPeerConnection`:
|
||||||
|
- `on_signaling_state_change`;
|
||||||
|
- `on_data_channel`;
|
||||||
|
- `on_negotiation_needed`;
|
||||||
|
- `on_ice_candidate`;
|
||||||
|
- `on_ice_gathering_state_change`;
|
||||||
|
- `on_track`;
|
||||||
|
- `on_ice_connection_state_change`;
|
||||||
|
- `on_peer_connection_state_change`.
|
||||||
|
- `RTCSctpTransport`:
|
||||||
|
- `on_error`;
|
||||||
|
- `on_data_channel`;
|
||||||
|
- `on_data_channel_opened`.
|
||||||
|
|
||||||
|
[#333]: https://github.com/webrtc-rs/webrtc/pull/333
|
||||||
|
|
||||||
|
## v0.5.1
|
||||||
|
|
||||||
|
* Promote agent lock in ice_gather.rs create_agent() to top level of the function to avoid a race condition. [#290 Promote create_agent lock to top of function, to avoid race condition](https://github.com/webrtc-rs/webrtc/pull/290) contributed by [efer-ms](https://github.com/efer-ms)
|
||||||
|
|
||||||
|
## v0.5.0
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
|
||||||
|
#### Breaking changes
|
||||||
|
|
||||||
|
* The serialized format for `RTCIceCandidateInit` has changed to match what the specification i.e. keys are camelCase. [#153 Make RTCIceCandidateInit conform to WebRTC spec](https://github.com/webrtc-rs/webrtc/pull/153) contributed by [jmatss](https://github.com/jmatss).
|
||||||
|
* Improved robustness when proposing RTP extension IDs and handling of collisions in these. This change is only breaking if you have assumed anything about the nature of these extension IDs. [#154 Fix RTP extension id collision](https://github.com/webrtc-rs/webrtc/pull/154) contributed by [k0nserv](https://github.com/k0nserv)
|
||||||
|
* Transceivers will now not stop when either or both directions are disabled. That is, applying and SDP with `a=inactive` will not stop the transceiver, instead attached senders and receivers will pause. A transceiver can be resurrected by setting direction back to e.g. `a=sendrecv`. The desired direction can be controlled with the newly introduced public method `set_direction` on `RTCRtpTransceiver`.
|
||||||
|
* [#201 Handle inactive transceivers more correctly](https://github.com/webrtc-rs/webrtc/pull/201) contributed by [k0nserv](https://github.com/k0nserv)
|
||||||
|
* [#210 Rework transceiver direction support further](https://github.com/webrtc-rs/webrtc/pull/210) contributed by [k0nserv](https://github.com/k0nserv)
|
||||||
|
* [#214 set_direction add missing Send + Sync bound](https://github.com/webrtc-rs/webrtc/pull/214) contributed by [algesten](https://github.com/algesten)
|
||||||
|
* [#213 set_direction add missing Sync bound](https://github.com/webrtc-rs/webrtc/pull/213) contributed by [algesten](https://github.com/algesten)
|
||||||
|
* [#212 Public RTCRtpTransceiver::set_direction](https://github.com/webrtc-rs/webrtc/pull/212) contributed by [algesten](https://github.com/algesten)
|
||||||
|
* [#268 Fix current direction update when applying answer](https://github.com/webrtc-rs/webrtc/pull/268) contributed by [k0nserv](https://github.com/k0nserv)
|
||||||
|
* [#236 Pause RTP writing if direction indicates it](https://github.com/webrtc-rs/webrtc/pull/236) contributed by [algesten](https://github.com/algesten)
|
||||||
|
* Generated the `a=msid` line for `m=` line sections according to the specification. This might be break remote peers that relied on the previous, incorrect, behaviour. This also fixes a bug where an endless negotiation loop could happen. [#217 Correct msid handling for RtpSender](https://github.com/webrtc-rs/webrtc/pull/217) contributed by [k0nserv](https://github.com/k0nserv)
|
||||||
|
* Improve data channel id negotiation. We've slightly adjust the public interface for creating pre-negotiated data channels. Instead of a separate `negotiated: Option<bool>` and `id: Option<u16>` in `RTCDataChannelInit` there's now a more idiomatic `negotiated: Option<u16>`. If you have a pre-negotiated data channel simply set `negotiated: Some(id)` when creating the data channel.
|
||||||
|
* [#237 Fix datachannel id setting for 0.5.0 release](https://github.com/webrtc-rs/webrtc/pull/237) contributed by [stuqdog](https://github.com/stuqdog)
|
||||||
|
* [#229 Revert "base id updating on whether it's been negotiated, not on its …](https://github.com/webrtc-rs/webrtc/pull/229) contributed by [melekes](https://github.com/melekes)
|
||||||
|
|
||||||
|
* [#226 base id updating on whether it's been finalized, not on its value](https://github.com/webrtc-rs/webrtc/pull/226) contributed by [stuqdog](https://github.com/stuqdog)
|
||||||
|
|
||||||
|
|
||||||
|
#### Other improvememnts
|
||||||
|
|
||||||
|
We made various improvements and fixes since 0.4.0, including merging all subcrates into a single git repo. The old crate repos are archived and all development will now happen in https://github.com/webrtc-rs/webrtc/.
|
||||||
|
|
||||||
|
* We now provide stats reporting via the standardized `RTCPeerConnection::get_stats` method.
|
||||||
|
* [#277 Implement Remote Inbound Stats](https://github.com/webrtc-rs/webrtc/pull/277) contributed by [k0nserv](https://github.com/k0nserv)
|
||||||
|
* [#220 Make stats types pub so they can be used directly](https://github.com/webrtc-rs/webrtc/pull/220) contributed by [k0nserv](https://github.com/k0nserv)
|
||||||
|
* [#225 Add RTP Stats to stats report](https://github.com/webrtc-rs/webrtc/pull/225) contributed by [k0nserv](https://github.com/k0nserv)
|
||||||
|
* [#189 Serialize stats](https://github.com/webrtc-rs/webrtc/pull/189) contributed by [sax](https://github.com/sax)
|
||||||
|
* [#180 Get stats from peer connection](https://github.com/webrtc-rs/webrtc/pull/180) contributed by [sax](https://github.com/sax)
|
||||||
|
|
||||||
|
* [#278 Fix async-global-executor](https://github.com/webrtc-rs/webrtc/pull/278) contributed by [k0nserv](https://github.com/k0nserv)
|
||||||
|
* [#276 relax regex version requirement](https://github.com/webrtc-rs/webrtc/pull/276) contributed by [melekes](https://github.com/melekes)
|
||||||
|
* [#244 Update README.md instructions after monorepo merge](https://github.com/webrtc-rs/webrtc/pull/244) contributed by [k0nserv](https://github.com/k0nserv)
|
||||||
|
* [#241 move profile to workspace](https://github.com/webrtc-rs/webrtc/pull/241) contributed by [xnorpx](https://github.com/xnorpx)
|
||||||
|
* [#240 Increase timeout to "fix" test breaking](https://github.com/webrtc-rs/webrtc/pull/240) contributed by [algesten](https://github.com/algesten)
|
||||||
|
* [#239 One repo (again)](https://github.com/webrtc-rs/webrtc/pull/239) contributed by [algesten](https://github.com/algesten)
|
||||||
|
* [#234 Fix recent clippy lints](https://github.com/webrtc-rs/webrtc/pull/234) contributed by [k0nserv](https://github.com/k0nserv)
|
||||||
|
* [#224 update call to DataChannel::accept as per data pr #14](https://github.com/webrtc-rs/webrtc/pull/224) contributed by [melekes](https://github.com/melekes)
|
||||||
|
* [#223 dtls_transport: always set remote certificate](https://github.com/webrtc-rs/webrtc/pull/223) contributed by [melekes](https://github.com/melekes)
|
||||||
|
* [#216 Lower case mime types for comparison in fmpt lines](https://github.com/webrtc-rs/webrtc/pull/216) contributed by [k0nserv](https://github.com/k0nserv)
|
||||||
|
* [#211 Helper to trigger negotiation_needed](https://github.com/webrtc-rs/webrtc/pull/211) contributed by [algesten](https://github.com/algesten)
|
||||||
|
* [#209 MID generator feature](https://github.com/webrtc-rs/webrtc/pull/209) contributed by [algesten](https://github.com/algesten)
|
||||||
|
* [#208 update deps + loosen some requirements](https://github.com/webrtc-rs/webrtc/pull/208) contributed by [melekes](https://github.com/melekes)
|
||||||
|
* [#205 data_channel: handle stream EOF](https://github.com/webrtc-rs/webrtc/pull/205) contributed by [melekes](https://github.com/melekes)
|
||||||
|
* [#204 [peer_connection] allow persistent certificates](https://github.com/webrtc-rs/webrtc/pull/204) contributed by [melekes](https://github.com/melekes)
|
||||||
|
* [#202 bugfix-Udp connection not close (reopen #174) #195](https://github.com/webrtc-rs/webrtc/pull/202) contributed by [shiqifeng2000](https://github.com/shiqifeng2000)
|
||||||
|
* [#199 Upgrade ICE to 0.7.0](https://github.com/webrtc-rs/webrtc/pull/199) contributed by [k0nserv](https://github.com/k0nserv)
|
||||||
|
* [#194 Add AV1 MimeType and RtpCodecParameters](https://github.com/webrtc-rs/webrtc/pull/194) contributed by [billylindeman](https://github.com/billylindeman)
|
||||||
|
* [#188 Improve operations debuggability](https://github.com/webrtc-rs/webrtc/pull/188) contributed by [k0nserv](https://github.com/k0nserv)
|
||||||
|
* [#187 Fix SDP for rejected tracks to conform to RFC](https://github.com/webrtc-rs/webrtc/pull/187) contributed by [k0nserv](https://github.com/k0nserv)
|
||||||
|
* [#185 Adding some debug and display traits](https://github.com/webrtc-rs/webrtc/pull/185) contributed by [sevensidedmarble](https://github.com/sevensidedmarble)
|
||||||
|
* [#179 Fix example names in README](https://github.com/webrtc-rs/webrtc/pull/179) contributed by [ethagnawl](https://github.com/ethagnawl)
|
||||||
|
* [#176 Time overflow armv7 workaround](https://github.com/webrtc-rs/webrtc/pull/176) contributed by [frjol](https://github.com/frjol)
|
||||||
|
* [#171 close DTLS conn upon err](https://github.com/webrtc-rs/webrtc/pull/171) contributed by [melekes](https://github.com/melekes)
|
||||||
|
* [#170 always start sctp](https://github.com/webrtc-rs/webrtc/pull/170) contributed by [melekes](https://github.com/melekes)
|
||||||
|
* [#167 Add offer/answer/pranswer constructors for RTCSessionDescription](https://github.com/webrtc-rs/webrtc/pull/167) contributed by [sax](https://github.com/sax)
|
||||||
|
|
||||||
|
#### Subcrate updates
|
||||||
|
|
||||||
|
The various sub-crates have been updated as follows:
|
||||||
|
|
||||||
|
* util: 0.5.3 => 0.6.0
|
||||||
|
* sdp: 0.5.1 => 0.5.2
|
||||||
|
* mdns: 0.4.2 => 0.5.0
|
||||||
|
* stun: 0.4.2 => 0.4.3
|
||||||
|
* turn: 0.5.3 => 0.6.0
|
||||||
|
* ice: 0.6.4 => 0.8.0
|
||||||
|
* dtls: 0.5.2 => 0.6.0
|
||||||
|
* rtcp: 0.6.5 => 0.7.0
|
||||||
|
* rtp: 0.6.5 => 0.6.7
|
||||||
|
* srtp: 0.8.9 => 0.9.0
|
||||||
|
* scpt: 0.4.3 => 0.6.1
|
||||||
|
* data: 0.3.3 => 0.5.0
|
||||||
|
* interceptor: 0.7.6 => 0.8.0
|
||||||
|
* media: 0.4.5 => 0.4.7
|
||||||
|
|
||||||
|
Their respective change logs are found in the old, now archived, repositories and within their respective `CHANGELOG.md` files in the monorepo.
|
||||||
|
|
||||||
|
### Contributors
|
||||||
|
|
||||||
|
A big thanks to all the contributors that have made this release happen:
|
||||||
|
|
||||||
|
* [morajabi](https://github.com/morajabi)
|
||||||
|
* [sax](https://github.com/sax)
|
||||||
|
* [ethagnawl](https://github.com/ethagnawl)
|
||||||
|
* [xnorpx](https://github.com/xnorpx)
|
||||||
|
* [frjol](https://github.com/frjol)
|
||||||
|
* [algesten](https://github.com/algesten)
|
||||||
|
* [shiqifeng2000](https://github.com/shiqifeng2000)
|
||||||
|
* [billylindeman](https://github.com/billylindeman)
|
||||||
|
* [sevensidedmarble](https://github.com/sevensidedmarble)
|
||||||
|
* [k0nserv](https://github.com/k0nserv)
|
||||||
|
* [stuqdog](https://github.com/stuqdog)
|
||||||
|
* [neonphog](https://github.com/neonphog)
|
||||||
|
* [melekes](https://github.com/melekes)
|
||||||
|
* [jmatss](https://github.com/jmatss)
|
||||||
|
|
||||||
|
|
||||||
|
## Prior to 0.5.0
|
||||||
|
|
||||||
|
Before 0.5.0 there was no changelog, previous changes are sometimes, but not always, available in the [GitHub Releases](https://github.com/webrtc-rs/webrtc/releases).
|
||||||
Vendored
+161
@@ -0,0 +1,161 @@
|
|||||||
|
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
|
||||||
|
#
|
||||||
|
# When uploading crates to the registry Cargo will automatically
|
||||||
|
# "normalize" Cargo.toml files for maximal compatibility
|
||||||
|
# with all versions of Cargo and also rewrite `path` dependencies
|
||||||
|
# to registry (e.g., crates.io) dependencies.
|
||||||
|
#
|
||||||
|
# If you are reading this file be aware that the original Cargo.toml
|
||||||
|
# will likely look very different (and much more reasonable).
|
||||||
|
# See Cargo.toml.orig for the original contents.
|
||||||
|
|
||||||
|
[package]
|
||||||
|
edition = "2021"
|
||||||
|
name = "webrtc"
|
||||||
|
version = "0.10.1"
|
||||||
|
authors = ["Rain Liu <[email protected]>"]
|
||||||
|
description = "A pure Rust implementation of WebRTC API"
|
||||||
|
homepage = "https://webrtc.rs"
|
||||||
|
documentation = "https://docs.rs/webrtc"
|
||||||
|
readme = "README.md"
|
||||||
|
license = "MIT OR Apache-2.0"
|
||||||
|
repository = "https://github.com/webrtc-rs/webrtc"
|
||||||
|
|
||||||
|
[dependencies.arc-swap]
|
||||||
|
version = "1"
|
||||||
|
|
||||||
|
[dependencies.async-trait]
|
||||||
|
version = "0.1"
|
||||||
|
|
||||||
|
[dependencies.bytes]
|
||||||
|
version = "1"
|
||||||
|
|
||||||
|
[dependencies.cfg-if]
|
||||||
|
version = "1"
|
||||||
|
|
||||||
|
[dependencies.data]
|
||||||
|
version = "0.8.1"
|
||||||
|
package = "webrtc-data"
|
||||||
|
|
||||||
|
[dependencies.dtls]
|
||||||
|
version = "0.9.0"
|
||||||
|
package = "webrtc-dtls"
|
||||||
|
|
||||||
|
[dependencies.hex]
|
||||||
|
version = "0.4"
|
||||||
|
|
||||||
|
[dependencies.ice]
|
||||||
|
version = "0.10.1"
|
||||||
|
package = "webrtc-ice"
|
||||||
|
|
||||||
|
[dependencies.interceptor]
|
||||||
|
version = "0.11.0"
|
||||||
|
|
||||||
|
[dependencies.lazy_static]
|
||||||
|
version = "1.4"
|
||||||
|
|
||||||
|
[dependencies.log]
|
||||||
|
version = "0.4"
|
||||||
|
|
||||||
|
[dependencies.mdns]
|
||||||
|
version = "0.6.1"
|
||||||
|
package = "webrtc-mdns"
|
||||||
|
|
||||||
|
[dependencies.media]
|
||||||
|
version = "0.7.1"
|
||||||
|
package = "webrtc-media"
|
||||||
|
|
||||||
|
[dependencies.pem]
|
||||||
|
version = "3"
|
||||||
|
optional = true
|
||||||
|
|
||||||
|
[dependencies.rand]
|
||||||
|
version = "0.8"
|
||||||
|
|
||||||
|
[dependencies.rcgen]
|
||||||
|
version = "0.11"
|
||||||
|
features = [
|
||||||
|
"pem",
|
||||||
|
"x509-parser",
|
||||||
|
]
|
||||||
|
|
||||||
|
[dependencies.regex]
|
||||||
|
version = "1.9.5"
|
||||||
|
|
||||||
|
[dependencies.ring]
|
||||||
|
version = "0.17"
|
||||||
|
|
||||||
|
[dependencies.rtcp]
|
||||||
|
version = "0.10.1"
|
||||||
|
|
||||||
|
[dependencies.rtp]
|
||||||
|
version = "0.10.0"
|
||||||
|
|
||||||
|
[dependencies.rustls]
|
||||||
|
version = "0.21"
|
||||||
|
features = ["dangerous_configuration"]
|
||||||
|
|
||||||
|
[dependencies.sctp]
|
||||||
|
version = "0.9.1"
|
||||||
|
package = "webrtc-sctp"
|
||||||
|
|
||||||
|
[dependencies.sdp]
|
||||||
|
version = "0.6.1"
|
||||||
|
|
||||||
|
[dependencies.serde]
|
||||||
|
version = "1"
|
||||||
|
features = ["derive"]
|
||||||
|
|
||||||
|
[dependencies.serde_json]
|
||||||
|
version = "1"
|
||||||
|
|
||||||
|
[dependencies.sha2]
|
||||||
|
version = "0.10"
|
||||||
|
|
||||||
|
[dependencies.smol_str]
|
||||||
|
version = "0.2"
|
||||||
|
features = ["serde"]
|
||||||
|
|
||||||
|
[dependencies.srtp]
|
||||||
|
version = "0.12.0"
|
||||||
|
package = "webrtc-srtp"
|
||||||
|
|
||||||
|
[dependencies.stun]
|
||||||
|
version = "0.5.1"
|
||||||
|
|
||||||
|
[dependencies.thiserror]
|
||||||
|
version = "1"
|
||||||
|
|
||||||
|
[dependencies.time]
|
||||||
|
version = "0.3"
|
||||||
|
|
||||||
|
[dependencies.tokio]
|
||||||
|
version = "1.32.0"
|
||||||
|
features = ["full"]
|
||||||
|
|
||||||
|
[dependencies.turn]
|
||||||
|
version = "0.7.1"
|
||||||
|
|
||||||
|
[dependencies.url]
|
||||||
|
version = "2"
|
||||||
|
|
||||||
|
[dependencies.util]
|
||||||
|
version = "0.8.1"
|
||||||
|
package = "webrtc-util"
|
||||||
|
|
||||||
|
[dependencies.waitgroup]
|
||||||
|
version = "0.1"
|
||||||
|
|
||||||
|
[dev-dependencies.env_logger]
|
||||||
|
version = "0.10"
|
||||||
|
|
||||||
|
[dev-dependencies.tokio-test]
|
||||||
|
version = "0.4"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
openssl = ["srtp/openssl"]
|
||||||
|
pem = [
|
||||||
|
"dep:pem",
|
||||||
|
"dtls/pem",
|
||||||
|
]
|
||||||
|
vendored-openssl = ["srtp/vendored-openssl"]
|
||||||
+59
@@ -0,0 +1,59 @@
|
|||||||
|
[package]
|
||||||
|
name = "webrtc"
|
||||||
|
version = "0.10.1"
|
||||||
|
authors = ["Rain Liu <[email protected]>"]
|
||||||
|
edition = "2021"
|
||||||
|
description = "A pure Rust implementation of WebRTC API"
|
||||||
|
license = "MIT OR Apache-2.0"
|
||||||
|
documentation = "https://docs.rs/webrtc"
|
||||||
|
homepage = "https://webrtc.rs"
|
||||||
|
repository = "https://github.com/webrtc-rs/webrtc"
|
||||||
|
readme = "../README.md"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
data = { version = "0.8.1", path = "../data", package = "webrtc-data" }
|
||||||
|
dtls = { version = "0.9.0", path = "../dtls", package = "webrtc-dtls" }
|
||||||
|
ice = { version = "0.10.1", path = "../ice", package = "webrtc-ice" }
|
||||||
|
interceptor = { version = "0.11.0", path = "../interceptor" }
|
||||||
|
mdns = { version = "0.6.1", path = "../mdns", package = "webrtc-mdns" }
|
||||||
|
media = { version = "0.7.1", path = "../media", package = "webrtc-media" }
|
||||||
|
rtcp = { version = "0.10.1", path = "../rtcp" }
|
||||||
|
rtp = { version = "0.10.0", path = "../rtp" }
|
||||||
|
sctp = { version = "0.9.1", path = "../sctp", package = "webrtc-sctp" }
|
||||||
|
sdp = { version = "0.6.1", path = "../sdp" }
|
||||||
|
srtp = { version = "0.12.0", path = "../srtp", package = "webrtc-srtp" }
|
||||||
|
stun = { version = "0.5.1", path = "../stun" }
|
||||||
|
turn = { version = "0.7.1", path = "../turn" }
|
||||||
|
util = { version = "0.8.1", path = "../util", package = "webrtc-util" }
|
||||||
|
|
||||||
|
arc-swap = "1"
|
||||||
|
tokio = { version = "1.32.0", features = ["full"] }
|
||||||
|
log = "0.4"
|
||||||
|
async-trait = "0.1"
|
||||||
|
serde = { version = "1", features = ["derive"] }
|
||||||
|
serde_json = "1"
|
||||||
|
rand = "0.8"
|
||||||
|
bytes = "1"
|
||||||
|
thiserror = "1"
|
||||||
|
waitgroup = "0.1"
|
||||||
|
regex = "1.9.5"
|
||||||
|
smol_str = { version = "0.2", features = ["serde"] }
|
||||||
|
url = "2"
|
||||||
|
rustls = { version = "0.21", features = ["dangerous_configuration"]}
|
||||||
|
rcgen = { version = "0.11", features = ["pem", "x509-parser"]}
|
||||||
|
ring = "0.17"
|
||||||
|
sha2 = "0.10"
|
||||||
|
lazy_static = "1.4"
|
||||||
|
hex = "0.4"
|
||||||
|
pem = { version = "3", optional = true }
|
||||||
|
time = "0.3"
|
||||||
|
cfg-if = "1"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
tokio-test = "0.4"
|
||||||
|
env_logger = "0.10"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
pem = ["dep:pem", "dtls/pem"]
|
||||||
|
openssl = ["srtp/openssl"]
|
||||||
|
vendored-openssl = ["srtp/vendored-openssl"]
|
||||||
Vendored
+201
@@ -0,0 +1,201 @@
|
|||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright [yyyy] [name of copyright owner]
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
Vendored
+21
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2021 WebRTC.rs
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
Vendored
+144
@@ -0,0 +1,144 @@
|
|||||||
|
<h1 align="center">
|
||||||
|
<a href="https://webrtc.rs"><img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/webrtc.rs.png" alt="WebRTC.rs"></a>
|
||||||
|
<br>
|
||||||
|
</h1>
|
||||||
|
<p align="center">
|
||||||
|
<a href="https://github.com/webrtc-rs/webrtc/actions">
|
||||||
|
<img src="https://github.com/webrtc-rs/webrtc/workflows/cargo/badge.svg?branch=master">
|
||||||
|
</a>
|
||||||
|
<a href="https://codecov.io/gh/webrtc-rs/webrtc">
|
||||||
|
<img src="https://codecov.io/gh/webrtc-rs/webrtc/branch/master/graph/badge.svg">
|
||||||
|
</a>
|
||||||
|
<a href="https://deps.rs/repo/github/webrtc-rs/webrtc">
|
||||||
|
<img src="https://deps.rs/repo/github/webrtc-rs/webrtc/status.svg">
|
||||||
|
</a>
|
||||||
|
<a href="https://crates.io/crates/webrtc">
|
||||||
|
<img src="https://img.shields.io/crates/v/webrtc.svg">
|
||||||
|
</a>
|
||||||
|
<a href="https://docs.rs/webrtc">
|
||||||
|
<img src="https://docs.rs/webrtc/badge.svg">
|
||||||
|
</a>
|
||||||
|
<a href="https://doc.rust-lang.org/1.6.0/complement-project-faq.html#why-dual-mitasl2-license">
|
||||||
|
<img src="https://img.shields.io/badge/license-MIT%2FApache--2.0-blue" alt="License: MIT/Apache 2.0">
|
||||||
|
</a>
|
||||||
|
<a href="https://discord.gg/4Ju8UHdXMs">
|
||||||
|
<img src="https://img.shields.io/discord/800204819540869120?logo=discord" alt="Discord">
|
||||||
|
</a>
|
||||||
|
<a href="https://twitter.com/WebRTCrs">
|
||||||
|
<img src="https://img.shields.io/twitter/url/https/twitter.com/webrtcrs.svg?style=social&label=%40WebRTCrs" alt="Twitter">
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
|
<p align="center">
|
||||||
|
A pure Rust implementation of WebRTC stack. Rewrite <a href="http://Pion.ly">Pion</a> WebRTC stack in Rust
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p align="center">
|
||||||
|
<strong>Sponsored with 💖 by</strong><br>
|
||||||
|
</p>
|
||||||
|
<p align="center">
|
||||||
|
<strong>Gold Sponsors:</strong><br>
|
||||||
|
<a href="https://www.parity.io/" target="_blank">
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/parity.png" style="height:75px;" alt="Parity Technologies">
|
||||||
|
</a><br>
|
||||||
|
<p align="center">
|
||||||
|
<strong>Silver Sponsors:</strong><br>
|
||||||
|
<a href="https://getstream.io/video/voice-calling/?utm_source=https://github.com/webrtc-rs/webrtc&utm_medium=sponsorship&utm_content=&utm_campaign=webrtcRepo_July2023_video_klmh22" target="_blank">
|
||||||
|
<img src="https://stream-blog-v2.imgix.net/blog/wp-content/uploads/f7401112f41742c4e173c30d4f318cb8/stream_logo_white.png?h=50" alt="Stream Chat">
|
||||||
|
</a><br>
|
||||||
|
<a href="https://kittycad.io/" target="_blank">
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/KittyCAD.png" alt="KittyCAD">
|
||||||
|
</a><br>
|
||||||
|
<strong>Bronze Sponsors:</strong><br>
|
||||||
|
<a href="https://www.embark-studios.com/" target="_blank"><img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/embark.jpg" alt="embark"></a><br>
|
||||||
|
<a href="https://github.com/AdrianEddy" target="_blank">AdrianEddy</a><br>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary><b>Table of Content</b></summary>
|
||||||
|
|
||||||
|
- [Overview](#overview)
|
||||||
|
- [Features](#features)
|
||||||
|
- [Building](#building)
|
||||||
|
- [Toolchain](#toolchain)
|
||||||
|
- [Monorepo Setup](#monorepo-setup)
|
||||||
|
- [Open Source License](#open-source-license)
|
||||||
|
- [Contributing](#contributing)
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
WebRTC.rs is a pure Rust implementation of WebRTC stack, which rewrites <a href="https://github.com/pion/webrtc/releases/tag/v3.1.5">Pion</a> stack in Rust.
|
||||||
|
This project is still in active and early development stage, please refer to the [Roadmap](https://github.com/webrtc-rs/webrtc/issues/1) to track the major milestones and releases.
|
||||||
|
[Examples](https://github.com/webrtc-rs/webrtc/blob/master/examples/examples/README.md) provide code samples to show how to use webrtc-rs to build media and data channel applications.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
<p align="center">
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/check.png">WebRTC<a href="https://crates.io/crates/webrtc"><img src="https://img.shields.io/crates/v/webrtc.svg"></a>
|
||||||
|
<br>
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/check.png">Media<a href="https://crates.io/crates/webrtc-media"><img src="https://img.shields.io/crates/v/webrtc-media.svg"></a>
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/check.png">Interceptor<a href="https://crates.io/crates/interceptor"><img src="https://img.shields.io/crates/v/interceptor.svg"></a>
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/check.png">Data<a href="https://crates.io/crates/webrtc-data"><img src="https://img.shields.io/crates/v/webrtc-data.svg"></a>
|
||||||
|
<br>
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/check.png">RTP<a href="https://crates.io/crates/rtp"><img src="https://img.shields.io/crates/v/rtp.svg"></a>
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/check.png">RTCP<a href="https://crates.io/crates/rtcp"><img src="https://img.shields.io/crates/v/rtcp.svg"></a>
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/check.png">SRTP<a href="https://crates.io/crates/webrtc-srtp"><img src="https://img.shields.io/crates/v/webrtc-srtp.svg"></a>
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/check.png">SCTP<a href="https://crates.io/crates/webrtc-sctp"><img src="https://img.shields.io/crates/v/webrtc-sctp.svg"></a>
|
||||||
|
<br>
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/check.png">DTLS<a href="https://crates.io/crates/webrtc-dtls"><img src="https://img.shields.io/crates/v/webrtc-dtls.svg"></a>
|
||||||
|
<br>
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/check.png">mDNS<a href="https://crates.io/crates/webrtc-mdns"><img src="https://img.shields.io/crates/v/webrtc-mdns.svg"></a>
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/check.png">STUN<a href="https://crates.io/crates/stun"><img src="https://img.shields.io/crates/v/stun.svg"></a>
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/check.png">TURN<a href="https://crates.io/crates/turn"><img src="https://img.shields.io/crates/v/turn.svg"></a>
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/check.png">ICE<a href="https://crates.io/crates/webrtc-ice"><img src="https://img.shields.io/crates/v/webrtc-ice.svg"></a>
|
||||||
|
<br>
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/check.png">SDP<a href="https://crates.io/crates/sdp"><img src="https://img.shields.io/crates/v/sdp.svg"></a>
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/check.png">Util<a href="https://crates.io/crates/webrtc-util"><img src="https://img.shields.io/crates/v/webrtc-util.svg"></a>
|
||||||
|
</p>
|
||||||
|
<p align="center">
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/webrtc_crates_dep_graph.png" alt="WebRTC Crates Dependency Graph">
|
||||||
|
</p>
|
||||||
|
<p align="center">
|
||||||
|
<img src="https://raw.githubusercontent.com/webrtc-rs/webrtc/master/doc/webrtc_stack.png" alt="WebRTC Stack">
|
||||||
|
</p>
|
||||||
|
|
||||||
|
## Building
|
||||||
|
|
||||||
|
### Toolchain
|
||||||
|
|
||||||
|
**Minimum Supported Rust Version:** `1.65.0`
|
||||||
|
|
||||||
|
Our minimum supported rust version(MSRV) policy is to support versions of the compiler released within the last six months. We don't eagerly bump the minimum version we support, instead the minimum will be bumped on a needed by needed basis, usually because downstream dependencies force us to.
|
||||||
|
|
||||||
|
**Note:** Changes to the minimum supported version are not consider breaking from a [semver](https://semver.org/) perspective.
|
||||||
|
|
||||||
|
### Monorepo Setup
|
||||||
|
|
||||||
|
All webrtc dependent crates and examples are included in this repository at the top level in a Cargo workspace.
|
||||||
|
|
||||||
|
To build all webrtc examples:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
cd examples
|
||||||
|
cargo test # build all examples (maybe very slow)
|
||||||
|
#[ or just build single example (much faster)
|
||||||
|
cargo build --example play-from-disk-vpx # build play-from-disk-vpx example only
|
||||||
|
cargo build --example play-from-disk-h264 # build play-from-disk-h264 example only
|
||||||
|
#...
|
||||||
|
#]
|
||||||
|
```
|
||||||
|
|
||||||
|
To build webrtc crate:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
cargo build [or clippy or test or fmt]
|
||||||
|
```
|
||||||
|
|
||||||
|
## Open Source License
|
||||||
|
|
||||||
|
Dual licensing under both MIT and Apache-2.0 is the currently accepted standard by the Rust language community and has been used for both the compiler and many public libraries since (see <https://doc.rust-lang.org/1.6.0/complement-project-faq.html#why-dual-mitasl2-license>). In order to match the community standards, webrtc-rs is using the dual MIT+Apache-2.0 license.
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
Contributors or Pull Requests are Welcome!!!
|
||||||
Vendored
+25
@@ -0,0 +1,25 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_new_api() -> Result<()> {
|
||||||
|
let mut s = SettingEngine::default();
|
||||||
|
s.detach_data_channels();
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_default_codecs()?;
|
||||||
|
|
||||||
|
let api = APIBuilder::new()
|
||||||
|
.with_setting_engine(s)
|
||||||
|
.with_media_engine(m)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
api.setting_engine.detach.data_channels,
|
||||||
|
"Failed to set settings engine"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!api.media_engine.audio_codecs.is_empty(),
|
||||||
|
"Failed to set media engine"
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -0,0 +1,278 @@
|
|||||||
|
/*TODO:
|
||||||
|
use super::*;
|
||||||
|
use crate::api::APIBuilder;
|
||||||
|
use crate::peer_connection::configuration::RTCConfiguration;
|
||||||
|
|
||||||
|
use bytes::Bytes;
|
||||||
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use interceptor::mock::mock_builder::MockBuilder;
|
||||||
|
use interceptor::mock::mock_interceptor::MockInterceptor;
|
||||||
|
use interceptor::stream_info::StreamInfo;
|
||||||
|
use interceptor::{Attributes, Interceptor, RTPWriter, RTPWriterFn};
|
||||||
|
|
||||||
|
// E2E test of the features of Interceptors
|
||||||
|
// * Assert an extension can be set on an outbound packet
|
||||||
|
// * Assert an extension can be read on an outbound packet
|
||||||
|
// * Assert that attributes set by an interceptor are returned to the Reader
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_peer_connection_interceptor() -> Result<()> {
|
||||||
|
let create_pc = || async {
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_default_codecs()?;
|
||||||
|
|
||||||
|
let mut ir = Registry::new();
|
||||||
|
|
||||||
|
let BindLocalStreamFn = |info: &StreamInfo,
|
||||||
|
writer: Arc<dyn RTPWriter + Send + Sync>|
|
||||||
|
-> Pin<
|
||||||
|
Box<dyn Future<Output = Arc<dyn RTPWriter + Send + Sync>> + Send + Sync>,
|
||||||
|
> {
|
||||||
|
let writer2 = Arc::clone(&writer);
|
||||||
|
Box::pin(async move {
|
||||||
|
Arc::new(RTPWriterFn(Box::new(
|
||||||
|
move |in_pkt: &rtp::packet::Packet,
|
||||||
|
attributes: &Attributes|
|
||||||
|
-> Pin<
|
||||||
|
Box<
|
||||||
|
dyn Future<Output = std::result::Result<usize, interceptor::Error>>
|
||||||
|
+ Send
|
||||||
|
+ Sync,
|
||||||
|
>,
|
||||||
|
> {
|
||||||
|
let writer3 = Arc::clone(&writer2);
|
||||||
|
let a = attributes.clone();
|
||||||
|
// set extension on outgoing packet
|
||||||
|
let mut out_pkt = in_pkt.clone();
|
||||||
|
out_pkt.header.extension = true;
|
||||||
|
out_pkt.header.extension_profile = 0xBEDE;
|
||||||
|
|
||||||
|
Box::pin(async move {
|
||||||
|
out_pkt
|
||||||
|
.header
|
||||||
|
.set_extension(2, Bytes::from_static(b"foo"))?;
|
||||||
|
//writer3.write(&out_pkt, &a).await
|
||||||
|
Ok(0)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
))) as Arc<dyn RTPWriter + Send + Sync>
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
BindRemoteStreamFn: func(_ *interceptor.StreamInfo, reader interceptor.RTPReader) interceptor.RTPReader {
|
||||||
|
return interceptor.RTPReaderFunc(func(b []byte, a interceptor.Attributes) (int, interceptor.Attributes, error) {
|
||||||
|
if a == nil {
|
||||||
|
a = interceptor.Attributes{}
|
||||||
|
}
|
||||||
|
|
||||||
|
a.Set("attribute", "value")
|
||||||
|
return reader.Read(b, a)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
let mock_builder = Box::new(MockBuilder {
|
||||||
|
build:
|
||||||
|
Box::new(
|
||||||
|
|_: &str| -> std::result::Result<
|
||||||
|
Arc<dyn Interceptor + Send + Sync>,
|
||||||
|
interceptor::Error,
|
||||||
|
> {
|
||||||
|
Ok(Arc::new(MockInterceptor {
|
||||||
|
..Default::default()
|
||||||
|
}))
|
||||||
|
},
|
||||||
|
),
|
||||||
|
});
|
||||||
|
let mock_builder = MockBuilder::new(
|
||||||
|
|_: &str| -> std::result::Result<
|
||||||
|
Arc<dyn Interceptor + Send + Sync>,
|
||||||
|
interceptor::Error,
|
||||||
|
> {
|
||||||
|
Ok(Arc::new(MockInterceptor {
|
||||||
|
..Default::default()
|
||||||
|
}))
|
||||||
|
},
|
||||||
|
);
|
||||||
|
ir.add(Box::new(mock_builder));
|
||||||
|
|
||||||
|
let api = APIBuilder::new()
|
||||||
|
.with_media_engine(m)
|
||||||
|
.with_interceptor_registry(ir)
|
||||||
|
.build();
|
||||||
|
api.new_peer_connection(RTCConfiguration::default()).await
|
||||||
|
};
|
||||||
|
|
||||||
|
let offerer = create_pc().await?;
|
||||||
|
let answerer = create_pc().await?;
|
||||||
|
|
||||||
|
track, err := NewTrackLocalStaticSample(RTPCodecCapability{MimeType: MimeTypeVP8}, "video", "pion")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
_, err = offerer.AddTrack(track)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
seenRTP, seenRTPCancel := context.WithCancel(context.Background())
|
||||||
|
answerer.OnTrack(func(track *TrackRemote, receiver *RTPReceiver) {
|
||||||
|
p, attributes, readErr := track.ReadRTP()
|
||||||
|
assert.NoError(t, readErr)
|
||||||
|
|
||||||
|
assert.Equal(t, p.Extension, true)
|
||||||
|
assert.Equal(t, "foo", string(p.GetExtension(2)))
|
||||||
|
assert.Equal(t, "value", attributes.Get("attribute"))
|
||||||
|
|
||||||
|
seenRTPCancel()
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.NoError(t, signalPair(offerer, answerer))
|
||||||
|
|
||||||
|
func() {
|
||||||
|
ticker := time.NewTicker(time.Millisecond * 20)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-seenRTP.Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
assert.NoError(t, track.WriteSample(media.Sample{Data: []byte{0x00}, Duration: time.Second}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
closePairNow(t, offerer, answerer)
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Interceptor_BindUnbind(t *testing.T) {
|
||||||
|
lim := test.TimeOut(time.Second * 10)
|
||||||
|
defer lim.Stop()
|
||||||
|
|
||||||
|
report := test.CheckRoutines(t)
|
||||||
|
defer report()
|
||||||
|
|
||||||
|
m := &MediaEngine{}
|
||||||
|
assert.NoError(t, m.RegisterDefaultCodecs())
|
||||||
|
|
||||||
|
var (
|
||||||
|
cntBindRTCPReader uint32
|
||||||
|
cntBindRTCPWriter uint32
|
||||||
|
cntBindLocalStream uint32
|
||||||
|
cntUnbindLocalStream uint32
|
||||||
|
cntBindRemoteStream uint32
|
||||||
|
cntUnbindRemoteStream uint32
|
||||||
|
cntClose uint32
|
||||||
|
)
|
||||||
|
mockInterceptor := &mock_interceptor.Interceptor{
|
||||||
|
BindRTCPReaderFn: func(reader interceptor.RTCPReader) interceptor.RTCPReader {
|
||||||
|
atomic.AddUint32(&cntBindRTCPReader, 1)
|
||||||
|
return reader
|
||||||
|
},
|
||||||
|
BindRTCPWriterFn: func(writer interceptor.RTCPWriter) interceptor.RTCPWriter {
|
||||||
|
atomic.AddUint32(&cntBindRTCPWriter, 1)
|
||||||
|
return writer
|
||||||
|
},
|
||||||
|
BindLocalStreamFn: func(i *interceptor.StreamInfo, writer interceptor.RTPWriter) interceptor.RTPWriter {
|
||||||
|
atomic.AddUint32(&cntBindLocalStream, 1)
|
||||||
|
return writer
|
||||||
|
},
|
||||||
|
UnbindLocalStreamFn: func(i *interceptor.StreamInfo) {
|
||||||
|
atomic.AddUint32(&cntUnbindLocalStream, 1)
|
||||||
|
},
|
||||||
|
BindRemoteStreamFn: func(i *interceptor.StreamInfo, reader interceptor.RTPReader) interceptor.RTPReader {
|
||||||
|
atomic.AddUint32(&cntBindRemoteStream, 1)
|
||||||
|
return reader
|
||||||
|
},
|
||||||
|
UnbindRemoteStreamFn: func(i *interceptor.StreamInfo) {
|
||||||
|
atomic.AddUint32(&cntUnbindRemoteStream, 1)
|
||||||
|
},
|
||||||
|
CloseFn: func() error {
|
||||||
|
atomic.AddUint32(&cntClose, 1)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
ir := &interceptor.Registry{}
|
||||||
|
ir.Add(&mock_interceptor.Factory{
|
||||||
|
NewInterceptorFn: func(_ string) (interceptor.Interceptor, error) { return mockInterceptor, nil },
|
||||||
|
})
|
||||||
|
|
||||||
|
sender, receiver, err := NewAPI(WithMediaEngine(m), WithInterceptorRegistry(ir)).newPair(Configuration{})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
track, err := NewTrackLocalStaticSample(RTPCodecCapability{MimeType: MimeTypeVP8}, "video", "pion")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
_, err = sender.AddTrack(track)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
receiverReady, receiverReadyFn := context.WithCancel(context.Background())
|
||||||
|
receiver.OnTrack(func(track *TrackRemote, _ *RTPReceiver) {
|
||||||
|
_, _, readErr := track.ReadRTP()
|
||||||
|
assert.NoError(t, readErr)
|
||||||
|
receiverReadyFn()
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.NoError(t, signalPair(sender, receiver))
|
||||||
|
|
||||||
|
ticker := time.NewTicker(time.Millisecond * 20)
|
||||||
|
defer ticker.Stop()
|
||||||
|
func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-receiverReady.Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
// Send packet to make receiver track actual creates RTPReceiver.
|
||||||
|
assert.NoError(t, track.WriteSample(media.Sample{Data: []byte{0xAA}, Duration: time.Second}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
closePairNow(t, sender, receiver)
|
||||||
|
|
||||||
|
// Bind/UnbindLocal/RemoteStream should be called from one side.
|
||||||
|
if cnt := atomic.LoadUint32(&cntBindLocalStream); cnt != 1 {
|
||||||
|
t.Errorf("BindLocalStreamFn is expected to be called once, but called %d times", cnt)
|
||||||
|
}
|
||||||
|
if cnt := atomic.LoadUint32(&cntUnbindLocalStream); cnt != 1 {
|
||||||
|
t.Errorf("UnbindLocalStreamFn is expected to be called once, but called %d times", cnt)
|
||||||
|
}
|
||||||
|
if cnt := atomic.LoadUint32(&cntBindRemoteStream); cnt != 1 {
|
||||||
|
t.Errorf("BindRemoteStreamFn is expected to be called once, but called %d times", cnt)
|
||||||
|
}
|
||||||
|
if cnt := atomic.LoadUint32(&cntUnbindRemoteStream); cnt != 1 {
|
||||||
|
t.Errorf("UnbindRemoteStreamFn is expected to be called once, but called %d times", cnt)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BindRTCPWriter/Reader and Close should be called from both side.
|
||||||
|
if cnt := atomic.LoadUint32(&cntBindRTCPWriter); cnt != 2 {
|
||||||
|
t.Errorf("BindRTCPWriterFn is expected to be called twice, but called %d times", cnt)
|
||||||
|
}
|
||||||
|
if cnt := atomic.LoadUint32(&cntBindRTCPReader); cnt != 2 {
|
||||||
|
t.Errorf("BindRTCPReaderFn is expected to be called twice, but called %d times", cnt)
|
||||||
|
}
|
||||||
|
if cnt := atomic.LoadUint32(&cntClose); cnt != 2 {
|
||||||
|
t.Errorf("CloseFn is expected to be called twice, but called %d times", cnt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_InterceptorRegistry_Build(t *testing.T) {
|
||||||
|
registryBuildCount := 0
|
||||||
|
|
||||||
|
ir := &interceptor.Registry{}
|
||||||
|
ir.Add(&mock_interceptor.Factory{
|
||||||
|
NewInterceptorFn: func(_ string) (interceptor.Interceptor, error) {
|
||||||
|
registryBuildCount++
|
||||||
|
return &interceptor.NoOp{}, nil
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
peerConnectionA, err := NewAPI(WithInterceptorRegistry(ir)).NewPeerConnection(Configuration{})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
peerConnectionB, err := NewAPI(WithInterceptorRegistry(ir)).NewPeerConnection(Configuration{})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, 2, registryBuildCount)
|
||||||
|
closePairNow(t, peerConnectionA, peerConnectionB)
|
||||||
|
}
|
||||||
|
*/
|
||||||
+171
@@ -0,0 +1,171 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
mod interceptor_registry_test;
|
||||||
|
|
||||||
|
use interceptor::nack::generator::Generator;
|
||||||
|
use interceptor::nack::responder::Responder;
|
||||||
|
use interceptor::registry::Registry;
|
||||||
|
use interceptor::report::receiver::ReceiverReport;
|
||||||
|
use interceptor::report::sender::SenderReport;
|
||||||
|
use interceptor::twcc::receiver::Receiver;
|
||||||
|
use interceptor::twcc::sender::Sender;
|
||||||
|
|
||||||
|
use crate::api::media_engine::MediaEngine;
|
||||||
|
use crate::error::Result;
|
||||||
|
use crate::rtp_transceiver::rtp_codec::{RTCRtpHeaderExtensionCapability, RTPCodecType};
|
||||||
|
use crate::rtp_transceiver::{RTCPFeedback, TYPE_RTCP_FB_TRANSPORT_CC};
|
||||||
|
|
||||||
|
/// register_default_interceptors will register some useful interceptors.
|
||||||
|
/// If you want to customize which interceptors are loaded, you should copy the
|
||||||
|
/// code from this method and remove unwanted interceptors.
|
||||||
|
pub fn register_default_interceptors(
|
||||||
|
mut registry: Registry,
|
||||||
|
media_engine: &mut MediaEngine,
|
||||||
|
) -> Result<Registry> {
|
||||||
|
registry = configure_nack(registry, media_engine);
|
||||||
|
|
||||||
|
registry = configure_rtcp_reports(registry);
|
||||||
|
|
||||||
|
registry = configure_twcc_receiver_only(registry, media_engine)?;
|
||||||
|
|
||||||
|
Ok(registry)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// configure_rtcp_reports will setup everything necessary for generating Sender and Receiver Reports
|
||||||
|
pub fn configure_rtcp_reports(mut registry: Registry) -> Registry {
|
||||||
|
let receiver = Box::new(ReceiverReport::builder());
|
||||||
|
let sender = Box::new(SenderReport::builder());
|
||||||
|
registry.add(receiver);
|
||||||
|
registry.add(sender);
|
||||||
|
registry
|
||||||
|
}
|
||||||
|
|
||||||
|
/// configure_nack will setup everything necessary for handling generating/responding to nack messages.
|
||||||
|
pub fn configure_nack(mut registry: Registry, media_engine: &mut MediaEngine) -> Registry {
|
||||||
|
media_engine.register_feedback(
|
||||||
|
RTCPFeedback {
|
||||||
|
typ: "nack".to_owned(),
|
||||||
|
parameter: "".to_owned(),
|
||||||
|
},
|
||||||
|
RTPCodecType::Video,
|
||||||
|
);
|
||||||
|
media_engine.register_feedback(
|
||||||
|
RTCPFeedback {
|
||||||
|
typ: "nack".to_owned(),
|
||||||
|
parameter: "pli".to_owned(),
|
||||||
|
},
|
||||||
|
RTPCodecType::Video,
|
||||||
|
);
|
||||||
|
|
||||||
|
let generator = Box::new(Generator::builder());
|
||||||
|
let responder = Box::new(Responder::builder());
|
||||||
|
registry.add(responder);
|
||||||
|
registry.add(generator);
|
||||||
|
registry
|
||||||
|
}
|
||||||
|
|
||||||
|
/// configure_twcc will setup everything necessary for adding
|
||||||
|
/// a TWCC header extension to outgoing RTP packets and generating TWCC reports.
|
||||||
|
pub fn configure_twcc(mut registry: Registry, media_engine: &mut MediaEngine) -> Result<Registry> {
|
||||||
|
media_engine.register_feedback(
|
||||||
|
RTCPFeedback {
|
||||||
|
typ: TYPE_RTCP_FB_TRANSPORT_CC.to_owned(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTPCodecType::Video,
|
||||||
|
);
|
||||||
|
media_engine.register_header_extension(
|
||||||
|
RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: sdp::extmap::TRANSPORT_CC_URI.to_owned(),
|
||||||
|
},
|
||||||
|
RTPCodecType::Video,
|
||||||
|
None,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
media_engine.register_feedback(
|
||||||
|
RTCPFeedback {
|
||||||
|
typ: TYPE_RTCP_FB_TRANSPORT_CC.to_owned(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTPCodecType::Audio,
|
||||||
|
);
|
||||||
|
media_engine.register_header_extension(
|
||||||
|
RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: sdp::extmap::TRANSPORT_CC_URI.to_owned(),
|
||||||
|
},
|
||||||
|
RTPCodecType::Audio,
|
||||||
|
None,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let sender = Box::new(Sender::builder());
|
||||||
|
let receiver = Box::new(Receiver::builder());
|
||||||
|
registry.add(sender);
|
||||||
|
registry.add(receiver);
|
||||||
|
Ok(registry)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// configure_twcc_sender will setup everything necessary for adding
|
||||||
|
/// a TWCC header extension to outgoing RTP packets. This will allow the remote peer to generate TWCC reports.
|
||||||
|
pub fn configure_twcc_sender_only(
|
||||||
|
mut registry: Registry,
|
||||||
|
media_engine: &mut MediaEngine,
|
||||||
|
) -> Result<Registry> {
|
||||||
|
media_engine.register_header_extension(
|
||||||
|
RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: sdp::extmap::TRANSPORT_CC_URI.to_owned(),
|
||||||
|
},
|
||||||
|
RTPCodecType::Video,
|
||||||
|
None,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
media_engine.register_header_extension(
|
||||||
|
RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: sdp::extmap::TRANSPORT_CC_URI.to_owned(),
|
||||||
|
},
|
||||||
|
RTPCodecType::Audio,
|
||||||
|
None,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let sender = Box::new(Sender::builder());
|
||||||
|
registry.add(sender);
|
||||||
|
Ok(registry)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// configure_twcc_receiver will setup everything necessary for generating TWCC reports.
|
||||||
|
pub fn configure_twcc_receiver_only(
|
||||||
|
mut registry: Registry,
|
||||||
|
media_engine: &mut MediaEngine,
|
||||||
|
) -> Result<Registry> {
|
||||||
|
media_engine.register_feedback(
|
||||||
|
RTCPFeedback {
|
||||||
|
typ: TYPE_RTCP_FB_TRANSPORT_CC.to_owned(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTPCodecType::Video,
|
||||||
|
);
|
||||||
|
media_engine.register_header_extension(
|
||||||
|
RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: sdp::extmap::TRANSPORT_CC_URI.to_owned(),
|
||||||
|
},
|
||||||
|
RTPCodecType::Video,
|
||||||
|
None,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
media_engine.register_feedback(
|
||||||
|
RTCPFeedback {
|
||||||
|
typ: TYPE_RTCP_FB_TRANSPORT_CC.to_owned(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTPCodecType::Audio,
|
||||||
|
);
|
||||||
|
media_engine.register_header_extension(
|
||||||
|
RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: sdp::extmap::TRANSPORT_CC_URI.to_owned(),
|
||||||
|
},
|
||||||
|
RTPCodecType::Audio,
|
||||||
|
None,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let receiver = Box::new(Receiver::builder());
|
||||||
|
registry.add(receiver);
|
||||||
|
Ok(registry)
|
||||||
|
}
|
||||||
@@ -0,0 +1,780 @@
|
|||||||
|
use std::io::Cursor;
|
||||||
|
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use crate::api::media_engine::MIME_TYPE_OPUS;
|
||||||
|
use crate::api::APIBuilder;
|
||||||
|
use crate::peer_connection::configuration::RTCConfiguration;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_opus_case() -> Result<()> {
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_default_codecs()?;
|
||||||
|
let api = APIBuilder::new().with_media_engine(m).build();
|
||||||
|
|
||||||
|
let pc = api.new_peer_connection(RTCConfiguration::default()).await?;
|
||||||
|
pc.add_transceiver_from_kind(RTPCodecType::Audio, None)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let offer = pc.create_offer(None).await?;
|
||||||
|
|
||||||
|
let re = Regex::new(r"(?m)^a=rtpmap:\d+ opus/48000/2").unwrap();
|
||||||
|
assert!(re.is_match(offer.sdp.as_str()));
|
||||||
|
|
||||||
|
pc.close().await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_video_case() -> Result<()> {
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_default_codecs()?;
|
||||||
|
let api = APIBuilder::new().with_media_engine(m).build();
|
||||||
|
|
||||||
|
let pc = api.new_peer_connection(RTCConfiguration::default()).await?;
|
||||||
|
pc.add_transceiver_from_kind(RTPCodecType::Video, None)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let offer = pc.create_offer(None).await?;
|
||||||
|
|
||||||
|
let re = Regex::new(r"(?m)^a=rtpmap:\d+ H264/90000").unwrap();
|
||||||
|
assert!(re.is_match(offer.sdp.as_str()));
|
||||||
|
let re = Regex::new(r"(?m)^a=rtpmap:\d+ VP8/90000").unwrap();
|
||||||
|
assert!(re.is_match(offer.sdp.as_str()));
|
||||||
|
let re = Regex::new(r"(?m)^a=rtpmap:\d+ VP9/90000").unwrap();
|
||||||
|
assert!(re.is_match(offer.sdp.as_str()));
|
||||||
|
|
||||||
|
pc.close().await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_media_engine_remote_description() -> Result<()> {
|
||||||
|
let must_parse = |raw: &str| -> Result<SessionDescription> {
|
||||||
|
let mut reader = Cursor::new(raw.as_bytes());
|
||||||
|
Ok(SessionDescription::unmarshal(&mut reader)?)
|
||||||
|
};
|
||||||
|
|
||||||
|
//"No Media"
|
||||||
|
{
|
||||||
|
const NO_MEDIA: &str = "v=0
|
||||||
|
o=- 4596489990601351948 2 IN IP4 127.0.0.1
|
||||||
|
s=-
|
||||||
|
t=0 0
|
||||||
|
";
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_default_codecs()?;
|
||||||
|
m.update_from_remote_description(&must_parse(NO_MEDIA)?)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
assert!(!m.negotiated_video.load(Ordering::SeqCst));
|
||||||
|
assert!(!m.negotiated_audio.load(Ordering::SeqCst));
|
||||||
|
}
|
||||||
|
|
||||||
|
//"Enable Opus"
|
||||||
|
{
|
||||||
|
const OPUS_SAME_PAYLOAD: &str = "v=0
|
||||||
|
o=- 4596489990601351948 2 IN IP4 127.0.0.1
|
||||||
|
s=-
|
||||||
|
t=0 0
|
||||||
|
m=audio 9 UDP/TLS/RTP/SAVPF 111
|
||||||
|
a=rtpmap:111 opus/48000/2
|
||||||
|
a=fmtp:111 minptime=10; useinbandfec=1
|
||||||
|
";
|
||||||
|
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_default_codecs()?;
|
||||||
|
m.update_from_remote_description(&must_parse(OPUS_SAME_PAYLOAD)?)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
assert!(!m.negotiated_video.load(Ordering::SeqCst));
|
||||||
|
assert!(m.negotiated_audio.load(Ordering::SeqCst));
|
||||||
|
|
||||||
|
let (opus_codec, _) = m.get_codec_by_payload(111).await?;
|
||||||
|
assert_eq!(opus_codec.capability.mime_type, MIME_TYPE_OPUS);
|
||||||
|
}
|
||||||
|
|
||||||
|
//"Change Payload Type"
|
||||||
|
{
|
||||||
|
const OPUS_SAME_PAYLOAD: &str = "v=0
|
||||||
|
o=- 4596489990601351948 2 IN IP4 127.0.0.1
|
||||||
|
s=-
|
||||||
|
t=0 0
|
||||||
|
m=audio 9 UDP/TLS/RTP/SAVPF 112
|
||||||
|
a=rtpmap:112 opus/48000/2
|
||||||
|
a=fmtp:112 minptime=10; useinbandfec=1
|
||||||
|
";
|
||||||
|
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_default_codecs()?;
|
||||||
|
m.update_from_remote_description(&must_parse(OPUS_SAME_PAYLOAD)?)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
assert!(!m.negotiated_video.load(Ordering::SeqCst));
|
||||||
|
assert!(m.negotiated_audio.load(Ordering::SeqCst));
|
||||||
|
|
||||||
|
let result = m.get_codec_by_payload(111).await;
|
||||||
|
assert!(result.is_err());
|
||||||
|
|
||||||
|
let (opus_codec, _) = m.get_codec_by_payload(112).await?;
|
||||||
|
assert_eq!(opus_codec.capability.mime_type, MIME_TYPE_OPUS);
|
||||||
|
}
|
||||||
|
|
||||||
|
//"Case Insensitive"
|
||||||
|
{
|
||||||
|
const OPUS_UPCASE: &str = "v=0
|
||||||
|
o=- 4596489990601351948 2 IN IP4 127.0.0.1
|
||||||
|
s=-
|
||||||
|
t=0 0
|
||||||
|
m=audio 9 UDP/TLS/RTP/SAVPF 111
|
||||||
|
a=rtpmap:111 OPUS/48000/2
|
||||||
|
a=fmtp:111 minptime=10; useinbandfec=1
|
||||||
|
";
|
||||||
|
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_default_codecs()?;
|
||||||
|
m.update_from_remote_description(&must_parse(OPUS_UPCASE)?)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
assert!(!m.negotiated_video.load(Ordering::SeqCst));
|
||||||
|
assert!(m.negotiated_audio.load(Ordering::SeqCst));
|
||||||
|
|
||||||
|
let (opus_codec, _) = m.get_codec_by_payload(111).await?;
|
||||||
|
assert_eq!(opus_codec.capability.mime_type, "audio/OPUS");
|
||||||
|
}
|
||||||
|
|
||||||
|
//"Handle different fmtp"
|
||||||
|
{
|
||||||
|
const OPUS_NO_FMTP: &str = "v=0
|
||||||
|
o=- 4596489990601351948 2 IN IP4 127.0.0.1
|
||||||
|
s=-
|
||||||
|
t=0 0
|
||||||
|
m=audio 9 UDP/TLS/RTP/SAVPF 111
|
||||||
|
a=rtpmap:111 opus/48000/2
|
||||||
|
";
|
||||||
|
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_default_codecs()?;
|
||||||
|
m.update_from_remote_description(&must_parse(OPUS_NO_FMTP)?)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
assert!(!m.negotiated_video.load(Ordering::SeqCst));
|
||||||
|
assert!(m.negotiated_audio.load(Ordering::SeqCst));
|
||||||
|
|
||||||
|
let (opus_codec, _) = m.get_codec_by_payload(111).await?;
|
||||||
|
assert_eq!(opus_codec.capability.mime_type, MIME_TYPE_OPUS);
|
||||||
|
}
|
||||||
|
|
||||||
|
//"Header Extensions"
|
||||||
|
{
|
||||||
|
const HEADER_EXTENSIONS: &str = "v=0
|
||||||
|
o=- 4596489990601351948 2 IN IP4 127.0.0.1
|
||||||
|
s=-
|
||||||
|
t=0 0
|
||||||
|
m=audio 9 UDP/TLS/RTP/SAVPF 111
|
||||||
|
a=extmap:7 urn:ietf:params:rtp-hdrext:sdes:mid
|
||||||
|
a=extmap:5 urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id
|
||||||
|
a=rtpmap:111 opus/48000/2
|
||||||
|
";
|
||||||
|
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_default_codecs()?;
|
||||||
|
for extension in [
|
||||||
|
"urn:ietf:params:rtp-hdrext:sdes:mid",
|
||||||
|
"urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id",
|
||||||
|
] {
|
||||||
|
m.register_header_extension(
|
||||||
|
RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: extension.to_owned(),
|
||||||
|
},
|
||||||
|
RTPCodecType::Audio,
|
||||||
|
None,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
m.update_from_remote_description(&must_parse(HEADER_EXTENSIONS)?)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
assert!(!m.negotiated_video.load(Ordering::SeqCst));
|
||||||
|
assert!(m.negotiated_audio.load(Ordering::SeqCst));
|
||||||
|
|
||||||
|
let (abs_id, abs_audio_enabled, abs_video_enabled) = m
|
||||||
|
.get_header_extension_id(RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: sdp::extmap::ABS_SEND_TIME_URI.to_owned(),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
assert_eq!(abs_id, 0);
|
||||||
|
assert!(!abs_audio_enabled);
|
||||||
|
assert!(!abs_video_enabled);
|
||||||
|
|
||||||
|
let (mid_id, mid_audio_enabled, mid_video_enabled) = m
|
||||||
|
.get_header_extension_id(RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: sdp::extmap::SDES_MID_URI.to_owned(),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
assert_eq!(mid_id, 7);
|
||||||
|
assert!(mid_audio_enabled);
|
||||||
|
assert!(!mid_video_enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
//"Prefers exact codec matches"
|
||||||
|
{
|
||||||
|
const PROFILE_LEVELS: &str = "v=0
|
||||||
|
o=- 4596489990601351948 2 IN IP4 127.0.0.1
|
||||||
|
s=-
|
||||||
|
t=0 0
|
||||||
|
m=video 60323 UDP/TLS/RTP/SAVPF 96 98
|
||||||
|
a=rtpmap:96 H264/90000
|
||||||
|
a=fmtp:96 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=640c1f
|
||||||
|
a=rtpmap:98 H264/90000
|
||||||
|
a=fmtp:98 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f
|
||||||
|
";
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_codec(
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_H264.to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line:
|
||||||
|
"level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f"
|
||||||
|
.to_string(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 127,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTPCodecType::Video,
|
||||||
|
)?;
|
||||||
|
m.update_from_remote_description(&must_parse(PROFILE_LEVELS)?)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
assert!(m.negotiated_video.load(Ordering::SeqCst));
|
||||||
|
assert!(!m.negotiated_audio.load(Ordering::SeqCst));
|
||||||
|
|
||||||
|
let (supported_h264, _) = m.get_codec_by_payload(98).await?;
|
||||||
|
assert_eq!(supported_h264.capability.mime_type, MIME_TYPE_H264);
|
||||||
|
|
||||||
|
assert!(m.get_codec_by_payload(96).await.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
//"Does not match when fmtpline is set and does not match"
|
||||||
|
{
|
||||||
|
const PROFILE_LEVELS: &str = "v=0
|
||||||
|
o=- 4596489990601351948 2 IN IP4 127.0.0.1
|
||||||
|
s=-
|
||||||
|
t=0 0
|
||||||
|
m=video 60323 UDP/TLS/RTP/SAVPF 96 98
|
||||||
|
a=rtpmap:96 H264/90000
|
||||||
|
a=fmtp:96 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=640c1f
|
||||||
|
";
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_codec(
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_H264.to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line:
|
||||||
|
"level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f"
|
||||||
|
.to_string(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 127,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTPCodecType::Video,
|
||||||
|
)?;
|
||||||
|
assert!(m
|
||||||
|
.update_from_remote_description(&must_parse(PROFILE_LEVELS)?)
|
||||||
|
.await
|
||||||
|
.is_err());
|
||||||
|
|
||||||
|
assert!(m.get_codec_by_payload(96).await.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
//"Matches when fmtpline is not set in offer, but exists in mediaengine"
|
||||||
|
{
|
||||||
|
const PROFILE_LEVELS: &str = "v=0
|
||||||
|
o=- 4596489990601351948 2 IN IP4 127.0.0.1
|
||||||
|
s=-
|
||||||
|
t=0 0
|
||||||
|
m=video 60323 UDP/TLS/RTP/SAVPF 96
|
||||||
|
a=rtpmap:96 VP9/90000
|
||||||
|
";
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_codec(
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_VP9.to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "profile-id=0".to_string(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 98,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTPCodecType::Video,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
m.update_from_remote_description(&must_parse(PROFILE_LEVELS)?)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
assert!(m.negotiated_video.load(Ordering::SeqCst));
|
||||||
|
|
||||||
|
m.get_codec_by_payload(96).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
//"Matches when fmtpline exists in neither"
|
||||||
|
{
|
||||||
|
const PROFILE_LEVELS: &str = "v=0
|
||||||
|
o=- 4596489990601351948 2 IN IP4 127.0.0.1
|
||||||
|
s=-
|
||||||
|
t=0 0
|
||||||
|
m=video 60323 UDP/TLS/RTP/SAVPF 96
|
||||||
|
a=rtpmap:96 VP8/90000
|
||||||
|
";
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_codec(
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_VP8.to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "".to_string(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 96,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTPCodecType::Video,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
m.update_from_remote_description(&must_parse(PROFILE_LEVELS)?)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
assert!(m.negotiated_video.load(Ordering::SeqCst));
|
||||||
|
|
||||||
|
m.get_codec_by_payload(96).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
//"Matches when rtx apt for exact match codec"
|
||||||
|
{
|
||||||
|
const PROFILE_LEVELS: &str = "v=0
|
||||||
|
o=- 4596489990601351948 2 IN IP4 127.0.0.1
|
||||||
|
s=-
|
||||||
|
t=0 0
|
||||||
|
m=video 60323 UDP/TLS/RTP/SAVPF 94 96 97
|
||||||
|
a=rtpmap:94 VP8/90000
|
||||||
|
a=rtpmap:96 VP9/90000
|
||||||
|
a=fmtp:96 profile-id=2
|
||||||
|
a=rtpmap:97 rtx/90000
|
||||||
|
a=fmtp:97 apt=96
|
||||||
|
";
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_codec(
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_VP8.to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "".to_string(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 94,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTPCodecType::Video,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
m.register_codec(
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_VP9.to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "profile-id=2".to_string(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 96,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTPCodecType::Video,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
m.register_codec(
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: "video/rtx".to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "apt=96".to_string(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 97,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTPCodecType::Video,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
m.update_from_remote_description(&must_parse(PROFILE_LEVELS)?)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
assert!(m.negotiated_video.load(Ordering::SeqCst));
|
||||||
|
|
||||||
|
m.get_codec_by_payload(97).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
//"Matches when rtx apt for partial match codec"
|
||||||
|
{
|
||||||
|
const PROFILE_LEVELS: &str = "v=0
|
||||||
|
o=- 4596489990601351948 2 IN IP4 127.0.0.1
|
||||||
|
s=-
|
||||||
|
t=0 0
|
||||||
|
m=video 60323 UDP/TLS/RTP/SAVPF 94 96 97
|
||||||
|
a=rtpmap:94 VP8/90000
|
||||||
|
a=rtpmap:96 VP9/90000
|
||||||
|
a=fmtp:96 profile-id=2
|
||||||
|
a=rtpmap:97 rtx/90000
|
||||||
|
a=fmtp:97 apt=96
|
||||||
|
";
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_codec(
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_VP8.to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "".to_string(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 94,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTPCodecType::Video,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
m.register_codec(
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_VP9.to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "profile-id=1".to_string(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 96,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTPCodecType::Video,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
m.register_codec(
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: "video/rtx".to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "apt=96".to_string(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 97,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTPCodecType::Video,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
m.update_from_remote_description(&must_parse(PROFILE_LEVELS)?)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
assert!(m.negotiated_video.load(Ordering::SeqCst));
|
||||||
|
|
||||||
|
if let Err(err) = m.get_codec_by_payload(97).await {
|
||||||
|
assert_eq!(err, Error::ErrCodecNotFound);
|
||||||
|
} else {
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_media_engine_header_extension_direction() -> Result<()> {
|
||||||
|
let register_codec = |m: &mut MediaEngine| -> Result<()> {
|
||||||
|
m.register_codec(
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_OPUS.to_owned(),
|
||||||
|
clock_rate: 48000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "".to_string(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 111,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTPCodecType::Audio,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
//"No Direction"
|
||||||
|
{
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
register_codec(&mut m)?;
|
||||||
|
m.register_header_extension(
|
||||||
|
RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: "webrtc-header-test".to_owned(),
|
||||||
|
},
|
||||||
|
RTPCodecType::Audio,
|
||||||
|
None,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let params =
|
||||||
|
m.get_rtp_parameters_by_kind(RTPCodecType::Audio, RTCRtpTransceiverDirection::Recvonly);
|
||||||
|
|
||||||
|
assert_eq!(params.header_extensions.len(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//"Same Direction"
|
||||||
|
{
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
register_codec(&mut m)?;
|
||||||
|
m.register_header_extension(
|
||||||
|
RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: "webrtc-header-test".to_owned(),
|
||||||
|
},
|
||||||
|
RTPCodecType::Audio,
|
||||||
|
Some(RTCRtpTransceiverDirection::Recvonly),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let params =
|
||||||
|
m.get_rtp_parameters_by_kind(RTPCodecType::Audio, RTCRtpTransceiverDirection::Recvonly);
|
||||||
|
|
||||||
|
assert_eq!(params.header_extensions.len(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//"Different Direction"
|
||||||
|
{
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
register_codec(&mut m)?;
|
||||||
|
m.register_header_extension(
|
||||||
|
RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: "webrtc-header-test".to_owned(),
|
||||||
|
},
|
||||||
|
RTPCodecType::Audio,
|
||||||
|
Some(RTCRtpTransceiverDirection::Sendonly),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let params =
|
||||||
|
m.get_rtp_parameters_by_kind(RTPCodecType::Audio, RTCRtpTransceiverDirection::Recvonly);
|
||||||
|
|
||||||
|
assert_eq!(params.header_extensions.len(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
//"No direction and inactive"
|
||||||
|
{
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
register_codec(&mut m)?;
|
||||||
|
m.register_header_extension(
|
||||||
|
RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: "webrtc-header-test".to_owned(),
|
||||||
|
},
|
||||||
|
RTPCodecType::Audio,
|
||||||
|
None,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let params =
|
||||||
|
m.get_rtp_parameters_by_kind(RTPCodecType::Audio, RTCRtpTransceiverDirection::Inactive);
|
||||||
|
|
||||||
|
assert_eq!(params.header_extensions.len(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// If a user attempts to register a codec twice we should just discard duplicate calls
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_media_engine_double_register() -> Result<()> {
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
|
||||||
|
m.register_codec(
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_OPUS.to_owned(),
|
||||||
|
clock_rate: 48000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "".to_string(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 111,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTPCodecType::Audio,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
m.register_codec(
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_OPUS.to_owned(),
|
||||||
|
clock_rate: 48000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "".to_string(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 111,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTPCodecType::Audio,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
assert_eq!(m.audio_codecs.len(), 1);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn validate(m: &MediaEngine) -> Result<()> {
|
||||||
|
m.update_header_extension(2, "test-extension", RTPCodecType::Audio)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let (id, audio_negotiated, video_negotiated) = m
|
||||||
|
.get_header_extension_id(RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: "test-extension".to_owned(),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
assert_eq!(id, 2);
|
||||||
|
assert!(audio_negotiated);
|
||||||
|
assert!(!video_negotiated);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The cloned MediaEngine instance should be able to update negotiated header extensions.
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_update_header_extension_to_cloned_media_engine() -> Result<()> {
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
|
||||||
|
m.register_codec(
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_OPUS.to_owned(),
|
||||||
|
clock_rate: 48000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "".to_string(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 111,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTPCodecType::Audio,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
m.register_header_extension(
|
||||||
|
RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: "test-extension".to_owned(),
|
||||||
|
},
|
||||||
|
RTPCodecType::Audio,
|
||||||
|
None,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
validate(&m).await?;
|
||||||
|
validate(&m.clone_to()).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_extension_id_collision() -> Result<()> {
|
||||||
|
let must_parse = |raw: &str| -> Result<SessionDescription> {
|
||||||
|
let mut reader = Cursor::new(raw.as_bytes());
|
||||||
|
Ok(SessionDescription::unmarshal(&mut reader)?)
|
||||||
|
};
|
||||||
|
|
||||||
|
const HEADER_EXTENSIONS: &str = "v=0
|
||||||
|
o=- 4596489990601351948 2 IN IP4 127.0.0.1
|
||||||
|
s=-
|
||||||
|
t=0 0
|
||||||
|
m=audio 9 UDP/TLS/RTP/SAVPF 111
|
||||||
|
a=extmap:7 urn:ietf:params:rtp-hdrext:sdes:mid
|
||||||
|
a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level
|
||||||
|
a=extmap:5 urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id
|
||||||
|
a=rtpmap:111 opus/48000/2
|
||||||
|
";
|
||||||
|
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_default_codecs()?;
|
||||||
|
{
|
||||||
|
let extension = "urn:3gpp:video-orientation";
|
||||||
|
m.register_header_extension(
|
||||||
|
RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: extension.to_owned(),
|
||||||
|
},
|
||||||
|
RTPCodecType::Video,
|
||||||
|
None,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
for extension in [
|
||||||
|
"urn:ietf:params:rtp-hdrext:ssrc-audio-level",
|
||||||
|
"urn:ietf:params:rtp-hdrext:sdes:mid",
|
||||||
|
"urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id",
|
||||||
|
] {
|
||||||
|
m.register_header_extension(
|
||||||
|
RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: extension.to_owned(),
|
||||||
|
},
|
||||||
|
RTPCodecType::Audio,
|
||||||
|
None,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
m.update_from_remote_description(&must_parse(HEADER_EXTENSIONS)?)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
assert!(!m.negotiated_video.load(Ordering::SeqCst));
|
||||||
|
assert!(m.negotiated_audio.load(Ordering::SeqCst));
|
||||||
|
|
||||||
|
let (abs_id, abs_audio_enabled, abs_video_enabled) = m
|
||||||
|
.get_header_extension_id(RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: sdp::extmap::ABS_SEND_TIME_URI.to_owned(),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
assert_eq!(abs_id, 0);
|
||||||
|
assert!(!abs_audio_enabled);
|
||||||
|
assert!(!abs_video_enabled);
|
||||||
|
|
||||||
|
let (mid_id, mid_audio_enabled, mid_video_enabled) = m
|
||||||
|
.get_header_extension_id(RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: sdp::extmap::SDES_MID_URI.to_owned(),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
assert_eq!(mid_id, 7);
|
||||||
|
assert!(mid_audio_enabled);
|
||||||
|
assert!(!mid_video_enabled);
|
||||||
|
|
||||||
|
let (mid_id, mid_audio_enabled, mid_video_enabled) = m
|
||||||
|
.get_header_extension_id(RTCRtpHeaderExtensionCapability {
|
||||||
|
uri: sdp::extmap::AUDIO_LEVEL_URI.to_owned(),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
assert_eq!(mid_id, 1);
|
||||||
|
assert!(mid_audio_enabled);
|
||||||
|
assert!(!mid_video_enabled);
|
||||||
|
|
||||||
|
let params =
|
||||||
|
m.get_rtp_parameters_by_kind(RTPCodecType::Video, RTCRtpTransceiverDirection::Sendonly);
|
||||||
|
//dbg!(¶ms);
|
||||||
|
|
||||||
|
let orientation = params
|
||||||
|
.header_extensions
|
||||||
|
.iter()
|
||||||
|
.find(|ext| ext.uri == "urn:3gpp:video-orientation")
|
||||||
|
.unwrap();
|
||||||
|
assert_ne!(orientation.id, 1);
|
||||||
|
assert_ne!(orientation.id, 7);
|
||||||
|
assert_ne!(orientation.id, 5);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
+804
@@ -0,0 +1,804 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
mod media_engine_test;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::ops::Range;
|
||||||
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
use sdp::description::session::SessionDescription;
|
||||||
|
use util::sync::Mutex as SyncMutex;
|
||||||
|
|
||||||
|
use crate::error::{Error, Result};
|
||||||
|
use crate::peer_connection::sdp::{
|
||||||
|
codecs_from_media_description, rtp_extensions_from_media_description,
|
||||||
|
};
|
||||||
|
use crate::rtp_transceiver::rtp_codec::{
|
||||||
|
codec_parameters_fuzzy_search, CodecMatch, RTCRtpCodecCapability, RTCRtpCodecParameters,
|
||||||
|
RTCRtpHeaderExtensionCapability, RTCRtpHeaderExtensionParameters, RTCRtpParameters,
|
||||||
|
RTPCodecType,
|
||||||
|
};
|
||||||
|
use crate::rtp_transceiver::rtp_transceiver_direction::RTCRtpTransceiverDirection;
|
||||||
|
use crate::rtp_transceiver::{fmtp, PayloadType, RTCPFeedback};
|
||||||
|
use crate::stats::stats_collector::StatsCollector;
|
||||||
|
use crate::stats::CodecStats;
|
||||||
|
use crate::stats::StatsReportType::Codec;
|
||||||
|
|
||||||
|
/// MIME_TYPE_H264 H264 MIME type.
|
||||||
|
/// Note: Matching should be case insensitive.
|
||||||
|
pub const MIME_TYPE_H264: &str = "video/H264";
|
||||||
|
/// MIME_TYPE_OPUS Opus MIME type
|
||||||
|
/// Note: Matching should be case insensitive.
|
||||||
|
pub const MIME_TYPE_OPUS: &str = "audio/opus";
|
||||||
|
/// MIME_TYPE_VP8 VP8 MIME type
|
||||||
|
/// Note: Matching should be case insensitive.
|
||||||
|
pub const MIME_TYPE_VP8: &str = "video/VP8";
|
||||||
|
/// MIME_TYPE_VP9 VP9 MIME type
|
||||||
|
/// Note: Matching should be case insensitive.
|
||||||
|
pub const MIME_TYPE_VP9: &str = "video/VP9";
|
||||||
|
/// MIME_TYPE_AV1 AV1 MIME type
|
||||||
|
/// Note: Matching should be case insensitive.
|
||||||
|
pub const MIME_TYPE_AV1: &str = "video/AV1";
|
||||||
|
/// MIME_TYPE_G722 G722 MIME type
|
||||||
|
/// Note: Matching should be case insensitive.
|
||||||
|
pub const MIME_TYPE_G722: &str = "audio/G722";
|
||||||
|
/// MIME_TYPE_PCMU PCMU MIME type
|
||||||
|
/// Note: Matching should be case insensitive.
|
||||||
|
pub const MIME_TYPE_PCMU: &str = "audio/PCMU";
|
||||||
|
/// MIME_TYPE_PCMA PCMA MIME type
|
||||||
|
/// Note: Matching should be case insensitive.
|
||||||
|
pub const MIME_TYPE_PCMA: &str = "audio/PCMA";
|
||||||
|
/// MIME_TYPE_TELEPHONE_EVENT telephone-event MIME type
|
||||||
|
/// Note: Matching should be case insensitive.
|
||||||
|
pub const MIME_TYPE_TELEPHONE_EVENT: &str = "audio/telephone-event";
|
||||||
|
|
||||||
|
const VALID_EXT_IDS: Range<isize> = 1..15;
|
||||||
|
|
||||||
|
#[derive(Default, Clone)]
|
||||||
|
pub(crate) struct MediaEngineHeaderExtension {
|
||||||
|
pub(crate) uri: String,
|
||||||
|
pub(crate) is_audio: bool,
|
||||||
|
pub(crate) is_video: bool,
|
||||||
|
pub(crate) allowed_direction: Option<RTCRtpTransceiverDirection>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MediaEngineHeaderExtension {
|
||||||
|
pub fn is_matching_direction(&self, dir: RTCRtpTransceiverDirection) -> bool {
|
||||||
|
if let Some(allowed_direction) = self.allowed_direction {
|
||||||
|
use RTCRtpTransceiverDirection::*;
|
||||||
|
allowed_direction == Inactive && dir == Inactive
|
||||||
|
|| allowed_direction.has_send() && dir.has_send()
|
||||||
|
|| allowed_direction.has_recv() && dir.has_recv()
|
||||||
|
} else {
|
||||||
|
// None means all directions matches.
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A MediaEngine defines the codecs supported by a PeerConnection, and the
|
||||||
|
/// configuration of those codecs. A MediaEngine must not be shared between
|
||||||
|
/// PeerConnections.
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct MediaEngine {
|
||||||
|
// If we have attempted to negotiate a codec type yet.
|
||||||
|
pub(crate) negotiated_video: AtomicBool,
|
||||||
|
pub(crate) negotiated_audio: AtomicBool,
|
||||||
|
|
||||||
|
pub(crate) video_codecs: Vec<RTCRtpCodecParameters>,
|
||||||
|
pub(crate) audio_codecs: Vec<RTCRtpCodecParameters>,
|
||||||
|
pub(crate) negotiated_video_codecs: SyncMutex<Vec<RTCRtpCodecParameters>>,
|
||||||
|
pub(crate) negotiated_audio_codecs: SyncMutex<Vec<RTCRtpCodecParameters>>,
|
||||||
|
|
||||||
|
header_extensions: Vec<MediaEngineHeaderExtension>,
|
||||||
|
proposed_header_extensions: SyncMutex<HashMap<isize, MediaEngineHeaderExtension>>,
|
||||||
|
pub(crate) negotiated_header_extensions: SyncMutex<HashMap<isize, MediaEngineHeaderExtension>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MediaEngine {
|
||||||
|
/// register_default_codecs registers the default codecs supported by Pion WebRTC.
|
||||||
|
/// register_default_codecs is not safe for concurrent use.
|
||||||
|
pub fn register_default_codecs(&mut self) -> Result<()> {
|
||||||
|
// Default Audio Codecs
|
||||||
|
for codec in vec![
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_OPUS.to_owned(),
|
||||||
|
clock_rate: 48000,
|
||||||
|
channels: 2,
|
||||||
|
sdp_fmtp_line: "minptime=10;useinbandfec=1".to_owned(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 111,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_G722.to_owned(),
|
||||||
|
clock_rate: 8000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "".to_owned(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 9,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_PCMU.to_owned(),
|
||||||
|
clock_rate: 8000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "".to_owned(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 0,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_PCMA.to_owned(),
|
||||||
|
clock_rate: 8000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "".to_owned(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 8,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
] {
|
||||||
|
self.register_codec(codec, RTPCodecType::Audio)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let video_rtcp_feedback = vec![
|
||||||
|
RTCPFeedback {
|
||||||
|
typ: "goog-remb".to_owned(),
|
||||||
|
parameter: "".to_owned(),
|
||||||
|
},
|
||||||
|
RTCPFeedback {
|
||||||
|
typ: "ccm".to_owned(),
|
||||||
|
parameter: "fir".to_owned(),
|
||||||
|
},
|
||||||
|
RTCPFeedback {
|
||||||
|
typ: "nack".to_owned(),
|
||||||
|
parameter: "".to_owned(),
|
||||||
|
},
|
||||||
|
RTCPFeedback {
|
||||||
|
typ: "nack".to_owned(),
|
||||||
|
parameter: "pli".to_owned(),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
for codec in vec![
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_VP8.to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "".to_owned(),
|
||||||
|
rtcp_feedback: video_rtcp_feedback.clone(),
|
||||||
|
},
|
||||||
|
payload_type: 96,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_VP9.to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "profile-id=0".to_owned(),
|
||||||
|
rtcp_feedback: video_rtcp_feedback.clone(),
|
||||||
|
},
|
||||||
|
payload_type: 98,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_VP9.to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "profile-id=1".to_owned(),
|
||||||
|
rtcp_feedback: video_rtcp_feedback.clone(),
|
||||||
|
},
|
||||||
|
payload_type: 100,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_H264.to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line:
|
||||||
|
"level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f"
|
||||||
|
.to_owned(),
|
||||||
|
rtcp_feedback: video_rtcp_feedback.clone(),
|
||||||
|
},
|
||||||
|
payload_type: 102,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_H264.to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line:
|
||||||
|
"level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42001f"
|
||||||
|
.to_owned(),
|
||||||
|
rtcp_feedback: video_rtcp_feedback.clone(),
|
||||||
|
},
|
||||||
|
payload_type: 127,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_H264.to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line:
|
||||||
|
"level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f"
|
||||||
|
.to_owned(),
|
||||||
|
rtcp_feedback: video_rtcp_feedback.clone(),
|
||||||
|
},
|
||||||
|
payload_type: 125,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_H264.to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line:
|
||||||
|
"level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42e01f"
|
||||||
|
.to_owned(),
|
||||||
|
rtcp_feedback: video_rtcp_feedback.clone(),
|
||||||
|
},
|
||||||
|
payload_type: 108,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_H264.to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line:
|
||||||
|
"level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42001f"
|
||||||
|
.to_owned(),
|
||||||
|
rtcp_feedback: video_rtcp_feedback.clone(),
|
||||||
|
},
|
||||||
|
payload_type: 127,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_H264.to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line:
|
||||||
|
"level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=640032"
|
||||||
|
.to_owned(),
|
||||||
|
rtcp_feedback: video_rtcp_feedback.clone(),
|
||||||
|
},
|
||||||
|
payload_type: 123,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: MIME_TYPE_AV1.to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "profile-id=0".to_owned(),
|
||||||
|
rtcp_feedback: video_rtcp_feedback,
|
||||||
|
},
|
||||||
|
payload_type: 41,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: "video/ulpfec".to_owned(),
|
||||||
|
clock_rate: 90000,
|
||||||
|
channels: 0,
|
||||||
|
sdp_fmtp_line: "".to_owned(),
|
||||||
|
rtcp_feedback: vec![],
|
||||||
|
},
|
||||||
|
payload_type: 116,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
] {
|
||||||
|
self.register_codec(codec, RTPCodecType::Video)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// add_codec will append codec if it not exists
|
||||||
|
fn add_codec(codecs: &mut Vec<RTCRtpCodecParameters>, codec: RTCRtpCodecParameters) {
|
||||||
|
for c in codecs.iter() {
|
||||||
|
if c.capability.mime_type == codec.capability.mime_type
|
||||||
|
&& c.payload_type == codec.payload_type
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
codecs.push(codec);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// register_codec adds codec to the MediaEngine
|
||||||
|
/// These are the list of codecs supported by this PeerConnection.
|
||||||
|
/// register_codec is not safe for concurrent use.
|
||||||
|
pub fn register_codec(
|
||||||
|
&mut self,
|
||||||
|
mut codec: RTCRtpCodecParameters,
|
||||||
|
typ: RTPCodecType,
|
||||||
|
) -> Result<()> {
|
||||||
|
codec.stats_id = format!(
|
||||||
|
"RTPCodec-{}",
|
||||||
|
SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.unwrap()
|
||||||
|
.as_nanos()
|
||||||
|
);
|
||||||
|
match typ {
|
||||||
|
RTPCodecType::Audio => {
|
||||||
|
MediaEngine::add_codec(&mut self.audio_codecs, codec);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
RTPCodecType::Video => {
|
||||||
|
MediaEngine::add_codec(&mut self.video_codecs, codec);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
_ => Err(Error::ErrUnknownType),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Adds a header extension to the MediaEngine
|
||||||
|
/// To determine the negotiated value use [`get_header_extension_id`] after signaling is complete.
|
||||||
|
///
|
||||||
|
/// The `allowed_direction` controls for which transceiver directions the extension matches. If
|
||||||
|
/// set to `None` it matches all directions. The `SendRecv` direction would match all transceiver
|
||||||
|
/// directions apart from `Inactive`. Inactive only matches inactive.
|
||||||
|
pub fn register_header_extension(
|
||||||
|
&mut self,
|
||||||
|
extension: RTCRtpHeaderExtensionCapability,
|
||||||
|
typ: RTPCodecType,
|
||||||
|
allowed_direction: Option<RTCRtpTransceiverDirection>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let ext = {
|
||||||
|
match self
|
||||||
|
.header_extensions
|
||||||
|
.iter_mut()
|
||||||
|
.find(|ext| ext.uri == extension.uri)
|
||||||
|
{
|
||||||
|
Some(ext) => ext,
|
||||||
|
None => {
|
||||||
|
// We have registered too many extensions
|
||||||
|
if self.header_extensions.len() > VALID_EXT_IDS.end as usize {
|
||||||
|
return Err(Error::ErrRegisterHeaderExtensionNoFreeID);
|
||||||
|
}
|
||||||
|
self.header_extensions.push(MediaEngineHeaderExtension {
|
||||||
|
allowed_direction,
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
|
||||||
|
// Unwrap is fine because we just pushed
|
||||||
|
self.header_extensions.last_mut().unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if typ == RTPCodecType::Audio {
|
||||||
|
ext.is_audio = true;
|
||||||
|
} else if typ == RTPCodecType::Video {
|
||||||
|
ext.is_video = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ext.uri = extension.uri;
|
||||||
|
|
||||||
|
if ext.allowed_direction != allowed_direction {
|
||||||
|
return Err(Error::ErrRegisterHeaderExtensionInvalidDirection);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// register_feedback adds feedback mechanism to already registered codecs.
|
||||||
|
pub fn register_feedback(&mut self, feedback: RTCPFeedback, typ: RTPCodecType) {
|
||||||
|
match typ {
|
||||||
|
RTPCodecType::Video => {
|
||||||
|
for v in &mut self.video_codecs {
|
||||||
|
v.capability.rtcp_feedback.push(feedback.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RTPCodecType::Audio => {
|
||||||
|
for a in &mut self.audio_codecs {
|
||||||
|
a.capability.rtcp_feedback.push(feedback.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// get_header_extension_id returns the negotiated ID for a header extension.
|
||||||
|
/// If the Header Extension isn't enabled ok will be false
|
||||||
|
pub(crate) async fn get_header_extension_id(
|
||||||
|
&self,
|
||||||
|
extension: RTCRtpHeaderExtensionCapability,
|
||||||
|
) -> (isize, bool, bool) {
|
||||||
|
let negotiated_header_extensions = self.negotiated_header_extensions.lock();
|
||||||
|
if negotiated_header_extensions.is_empty() {
|
||||||
|
return (0, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (id, h) in &*negotiated_header_extensions {
|
||||||
|
if extension.uri == h.uri {
|
||||||
|
return (*id, h.is_audio, h.is_video);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(0, false, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// clone_to copies any user modifiable state of the MediaEngine
|
||||||
|
/// all internal state is reset
|
||||||
|
pub(crate) fn clone_to(&self) -> Self {
|
||||||
|
MediaEngine {
|
||||||
|
video_codecs: self.video_codecs.clone(),
|
||||||
|
audio_codecs: self.audio_codecs.clone(),
|
||||||
|
header_extensions: self.header_extensions.clone(),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn get_codec_by_payload(
|
||||||
|
&self,
|
||||||
|
payload_type: PayloadType,
|
||||||
|
) -> Result<(RTCRtpCodecParameters, RTPCodecType)> {
|
||||||
|
{
|
||||||
|
let negotiated_video_codecs = self.negotiated_video_codecs.lock();
|
||||||
|
for codec in &*negotiated_video_codecs {
|
||||||
|
if codec.payload_type == payload_type {
|
||||||
|
return Ok((codec.clone(), RTPCodecType::Video));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let negotiated_audio_codecs = self.negotiated_audio_codecs.lock();
|
||||||
|
for codec in &*negotiated_audio_codecs {
|
||||||
|
if codec.payload_type == payload_type {
|
||||||
|
return Ok((codec.clone(), RTPCodecType::Audio));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(Error::ErrCodecNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn collect_stats(&self, collector: &StatsCollector) {
|
||||||
|
let mut reports = HashMap::new();
|
||||||
|
|
||||||
|
for codec in &self.video_codecs {
|
||||||
|
reports.insert(codec.stats_id.clone(), Codec(CodecStats::from(codec)));
|
||||||
|
}
|
||||||
|
|
||||||
|
for codec in &self.audio_codecs {
|
||||||
|
reports.insert(codec.stats_id.clone(), Codec(CodecStats::from(codec)));
|
||||||
|
}
|
||||||
|
|
||||||
|
collector.merge(reports);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Look up a codec and enable if it exists
|
||||||
|
pub(crate) fn match_remote_codec(
|
||||||
|
&self,
|
||||||
|
remote_codec: &RTCRtpCodecParameters,
|
||||||
|
typ: RTPCodecType,
|
||||||
|
exact_matches: &[RTCRtpCodecParameters],
|
||||||
|
partial_matches: &[RTCRtpCodecParameters],
|
||||||
|
) -> Result<CodecMatch> {
|
||||||
|
let codecs = if typ == RTPCodecType::Audio {
|
||||||
|
&self.audio_codecs
|
||||||
|
} else {
|
||||||
|
&self.video_codecs
|
||||||
|
};
|
||||||
|
|
||||||
|
let remote_fmtp = fmtp::parse(
|
||||||
|
&remote_codec.capability.mime_type,
|
||||||
|
remote_codec.capability.sdp_fmtp_line.as_str(),
|
||||||
|
);
|
||||||
|
if let Some(apt) = remote_fmtp.parameter("apt") {
|
||||||
|
let payload_type = apt.parse::<u8>()?;
|
||||||
|
|
||||||
|
let mut apt_match = CodecMatch::None;
|
||||||
|
for codec in exact_matches {
|
||||||
|
if codec.payload_type == payload_type {
|
||||||
|
apt_match = CodecMatch::Exact;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if apt_match == CodecMatch::None {
|
||||||
|
for codec in partial_matches {
|
||||||
|
if codec.payload_type == payload_type {
|
||||||
|
apt_match = CodecMatch::Partial;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if apt_match == CodecMatch::None {
|
||||||
|
return Ok(CodecMatch::None); // not an error, we just ignore this codec we don't support
|
||||||
|
}
|
||||||
|
|
||||||
|
// if apt's media codec is partial match, then apt codec must be partial match too
|
||||||
|
let (_, mut match_type) = codec_parameters_fuzzy_search(remote_codec, codecs);
|
||||||
|
if match_type == CodecMatch::Exact && apt_match == CodecMatch::Partial {
|
||||||
|
match_type = CodecMatch::Partial;
|
||||||
|
}
|
||||||
|
return Ok(match_type);
|
||||||
|
}
|
||||||
|
|
||||||
|
let (_, match_type) = codec_parameters_fuzzy_search(remote_codec, codecs);
|
||||||
|
Ok(match_type)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Look up a header extension and enable if it exists
|
||||||
|
pub(crate) async fn update_header_extension(
|
||||||
|
&self,
|
||||||
|
id: isize,
|
||||||
|
extension: &str,
|
||||||
|
typ: RTPCodecType,
|
||||||
|
) -> Result<()> {
|
||||||
|
let mut negotiated_header_extensions = self.negotiated_header_extensions.lock();
|
||||||
|
let mut proposed_header_extensions = self.proposed_header_extensions.lock();
|
||||||
|
|
||||||
|
for local_extension in &self.header_extensions {
|
||||||
|
if local_extension.uri != extension {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let negotiated_ext = negotiated_header_extensions
|
||||||
|
.iter_mut()
|
||||||
|
.find(|(_, ext)| ext.uri == extension);
|
||||||
|
|
||||||
|
if let Some(n_ext) = negotiated_ext {
|
||||||
|
if *n_ext.0 == id {
|
||||||
|
n_ext.1.is_video |= typ == RTPCodecType::Video;
|
||||||
|
n_ext.1.is_audio |= typ == RTPCodecType::Audio;
|
||||||
|
} else {
|
||||||
|
let nid = n_ext.0;
|
||||||
|
log::warn!("Invalid ext id mapping in update_header_extension. {} was negotiated as {}, but was {} in call", extension, nid, id);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// We either only have a proposal or we have neither proposal nor a negotiated id
|
||||||
|
// Accept whatevers the peer suggests
|
||||||
|
|
||||||
|
if let Some(prev_ext) = negotiated_header_extensions.get(&id) {
|
||||||
|
let prev_uri = &prev_ext.uri;
|
||||||
|
log::warn!("Assigning {} to {} would override previous assignment to {}, no action taken", id, extension, prev_uri);
|
||||||
|
} else {
|
||||||
|
let h = MediaEngineHeaderExtension {
|
||||||
|
uri: extension.to_owned(),
|
||||||
|
is_audio: local_extension.is_audio && typ == RTPCodecType::Audio,
|
||||||
|
is_video: local_extension.is_video && typ == RTPCodecType::Video,
|
||||||
|
allowed_direction: local_extension.allowed_direction,
|
||||||
|
};
|
||||||
|
negotiated_header_extensions.insert(id, h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear any proposals we had for this id
|
||||||
|
proposed_header_extensions.remove(&id);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn push_codecs(&self, codecs: Vec<RTCRtpCodecParameters>, typ: RTPCodecType) {
|
||||||
|
for codec in codecs {
|
||||||
|
if typ == RTPCodecType::Audio {
|
||||||
|
let mut negotiated_audio_codecs = self.negotiated_audio_codecs.lock();
|
||||||
|
MediaEngine::add_codec(&mut negotiated_audio_codecs, codec);
|
||||||
|
} else if typ == RTPCodecType::Video {
|
||||||
|
let mut negotiated_video_codecs = self.negotiated_video_codecs.lock();
|
||||||
|
MediaEngine::add_codec(&mut negotiated_video_codecs, codec);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update the MediaEngine from a remote description
|
||||||
|
pub(crate) async fn update_from_remote_description(
|
||||||
|
&self,
|
||||||
|
desc: &SessionDescription,
|
||||||
|
) -> Result<()> {
|
||||||
|
for media in &desc.media_descriptions {
|
||||||
|
let typ = if !self.negotiated_audio.load(Ordering::SeqCst)
|
||||||
|
&& media.media_name.media.to_lowercase() == "audio"
|
||||||
|
{
|
||||||
|
self.negotiated_audio.store(true, Ordering::SeqCst);
|
||||||
|
RTPCodecType::Audio
|
||||||
|
} else if !self.negotiated_video.load(Ordering::SeqCst)
|
||||||
|
&& media.media_name.media.to_lowercase() == "video"
|
||||||
|
{
|
||||||
|
self.negotiated_video.store(true, Ordering::SeqCst);
|
||||||
|
RTPCodecType::Video
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
let codecs = codecs_from_media_description(media)?;
|
||||||
|
|
||||||
|
let mut exact_matches = vec![]; //make([]RTPCodecParameters, 0, len(codecs))
|
||||||
|
let mut partial_matches = vec![]; //make([]RTPCodecParameters, 0, len(codecs))
|
||||||
|
|
||||||
|
for codec in codecs {
|
||||||
|
let match_type =
|
||||||
|
self.match_remote_codec(&codec, typ, &exact_matches, &partial_matches)?;
|
||||||
|
|
||||||
|
if match_type == CodecMatch::Exact {
|
||||||
|
exact_matches.push(codec);
|
||||||
|
} else if match_type == CodecMatch::Partial {
|
||||||
|
partial_matches.push(codec);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// use exact matches when they exist, otherwise fall back to partial
|
||||||
|
if !exact_matches.is_empty() {
|
||||||
|
self.push_codecs(exact_matches, typ).await;
|
||||||
|
} else if !partial_matches.is_empty() {
|
||||||
|
self.push_codecs(partial_matches, typ).await;
|
||||||
|
} else {
|
||||||
|
// no match, not negotiated
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let extensions = rtp_extensions_from_media_description(media)?;
|
||||||
|
|
||||||
|
for (extension, id) in extensions {
|
||||||
|
self.update_header_extension(id, &extension, typ).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn get_codecs_by_kind(&self, typ: RTPCodecType) -> Vec<RTCRtpCodecParameters> {
|
||||||
|
if typ == RTPCodecType::Video {
|
||||||
|
if self.negotiated_video.load(Ordering::SeqCst) {
|
||||||
|
let negotiated_video_codecs = self.negotiated_video_codecs.lock();
|
||||||
|
negotiated_video_codecs.clone()
|
||||||
|
} else {
|
||||||
|
self.video_codecs.clone()
|
||||||
|
}
|
||||||
|
} else if typ == RTPCodecType::Audio {
|
||||||
|
if self.negotiated_audio.load(Ordering::SeqCst) {
|
||||||
|
let negotiated_audio_codecs = self.negotiated_audio_codecs.lock();
|
||||||
|
negotiated_audio_codecs.clone()
|
||||||
|
} else {
|
||||||
|
self.audio_codecs.clone()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn get_rtp_parameters_by_kind(
|
||||||
|
&self,
|
||||||
|
typ: RTPCodecType,
|
||||||
|
direction: RTCRtpTransceiverDirection,
|
||||||
|
) -> RTCRtpParameters {
|
||||||
|
let mut header_extensions = vec![];
|
||||||
|
|
||||||
|
if self.negotiated_video.load(Ordering::SeqCst) && typ == RTPCodecType::Video
|
||||||
|
|| self.negotiated_audio.load(Ordering::SeqCst) && typ == RTPCodecType::Audio
|
||||||
|
{
|
||||||
|
let negotiated_header_extensions = self.negotiated_header_extensions.lock();
|
||||||
|
for (id, e) in &*negotiated_header_extensions {
|
||||||
|
if e.is_matching_direction(direction)
|
||||||
|
&& (e.is_audio && typ == RTPCodecType::Audio
|
||||||
|
|| e.is_video && typ == RTPCodecType::Video)
|
||||||
|
{
|
||||||
|
header_extensions.push(RTCRtpHeaderExtensionParameters {
|
||||||
|
id: *id,
|
||||||
|
uri: e.uri.clone(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let mut proposed_header_extensions = self.proposed_header_extensions.lock();
|
||||||
|
let mut negotiated_header_extensions = self.negotiated_header_extensions.lock();
|
||||||
|
|
||||||
|
for local_extension in &self.header_extensions {
|
||||||
|
let relevant = local_extension.is_matching_direction(direction)
|
||||||
|
&& (local_extension.is_audio && typ == RTPCodecType::Audio
|
||||||
|
|| local_extension.is_video && typ == RTPCodecType::Video);
|
||||||
|
|
||||||
|
if !relevant {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some((id, negotiated_extension)) = negotiated_header_extensions
|
||||||
|
.iter_mut()
|
||||||
|
.find(|(_, e)| e.uri == local_extension.uri)
|
||||||
|
{
|
||||||
|
// We have previously negotiated this extension, make sure to record it as
|
||||||
|
// active for the current type
|
||||||
|
negotiated_extension.is_audio |= typ == RTPCodecType::Audio;
|
||||||
|
negotiated_extension.is_video |= typ == RTPCodecType::Video;
|
||||||
|
|
||||||
|
header_extensions.push(RTCRtpHeaderExtensionParameters {
|
||||||
|
id: *id,
|
||||||
|
uri: negotiated_extension.uri.clone(),
|
||||||
|
});
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some((id, negotiated_extension)) = proposed_header_extensions
|
||||||
|
.iter_mut()
|
||||||
|
.find(|(_, e)| e.uri == local_extension.uri)
|
||||||
|
{
|
||||||
|
// We have previously proposed this extension, re-use it
|
||||||
|
header_extensions.push(RTCRtpHeaderExtensionParameters {
|
||||||
|
id: *id,
|
||||||
|
uri: negotiated_extension.uri.clone(),
|
||||||
|
});
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Figure out which (unused id) to propose.
|
||||||
|
let id = VALID_EXT_IDS.clone().find(|id| {
|
||||||
|
!negotiated_header_extensions.keys().any(|nid| nid == id)
|
||||||
|
&& !proposed_header_extensions.keys().any(|pid| pid == id)
|
||||||
|
});
|
||||||
|
|
||||||
|
if let Some(id) = id {
|
||||||
|
proposed_header_extensions.insert(
|
||||||
|
id,
|
||||||
|
MediaEngineHeaderExtension {
|
||||||
|
uri: local_extension.uri.clone(),
|
||||||
|
is_audio: local_extension.is_audio,
|
||||||
|
is_video: local_extension.is_video,
|
||||||
|
allowed_direction: local_extension.allowed_direction,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
header_extensions.push(RTCRtpHeaderExtensionParameters {
|
||||||
|
id,
|
||||||
|
uri: local_extension.uri.clone(),
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
log::warn!("No available RTP extension ID for {}", local_extension.uri);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
RTCRtpParameters {
|
||||||
|
header_extensions,
|
||||||
|
codecs: self.get_codecs_by_kind(typ),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn get_rtp_parameters_by_payload_type(
|
||||||
|
&self,
|
||||||
|
payload_type: PayloadType,
|
||||||
|
) -> Result<RTCRtpParameters> {
|
||||||
|
let (codec, typ) = self.get_codec_by_payload(payload_type).await?;
|
||||||
|
|
||||||
|
let mut header_extensions = vec![];
|
||||||
|
{
|
||||||
|
let negotiated_header_extensions = self.negotiated_header_extensions.lock();
|
||||||
|
for (id, e) in &*negotiated_header_extensions {
|
||||||
|
if e.is_audio && typ == RTPCodecType::Audio
|
||||||
|
|| e.is_video && typ == RTPCodecType::Video
|
||||||
|
{
|
||||||
|
header_extensions.push(RTCRtpHeaderExtensionParameters {
|
||||||
|
uri: e.uri.clone(),
|
||||||
|
id: *id,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(RTCRtpParameters {
|
||||||
|
header_extensions,
|
||||||
|
codecs: vec![codec],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Vendored
+236
@@ -0,0 +1,236 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
mod api_test;
|
||||||
|
|
||||||
|
pub mod interceptor_registry;
|
||||||
|
pub mod media_engine;
|
||||||
|
pub mod setting_engine;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::SystemTime;
|
||||||
|
|
||||||
|
use interceptor::registry::Registry;
|
||||||
|
use interceptor::Interceptor;
|
||||||
|
use media_engine::*;
|
||||||
|
use rcgen::KeyPair;
|
||||||
|
use setting_engine::*;
|
||||||
|
|
||||||
|
use crate::data_channel::data_channel_parameters::DataChannelParameters;
|
||||||
|
use crate::data_channel::RTCDataChannel;
|
||||||
|
use crate::dtls_transport::RTCDtlsTransport;
|
||||||
|
use crate::error::{Error, Result};
|
||||||
|
use crate::ice_transport::ice_gatherer::{RTCIceGatherOptions, RTCIceGatherer};
|
||||||
|
use crate::ice_transport::RTCIceTransport;
|
||||||
|
use crate::peer_connection::certificate::RTCCertificate;
|
||||||
|
use crate::peer_connection::configuration::RTCConfiguration;
|
||||||
|
use crate::peer_connection::RTCPeerConnection;
|
||||||
|
use crate::rtp_transceiver::rtp_codec::RTPCodecType;
|
||||||
|
use crate::rtp_transceiver::rtp_receiver::RTCRtpReceiver;
|
||||||
|
use crate::rtp_transceiver::rtp_sender::RTCRtpSender;
|
||||||
|
use crate::sctp_transport::RTCSctpTransport;
|
||||||
|
use crate::track::track_local::TrackLocal;
|
||||||
|
|
||||||
|
/// API bundles the global functions of the WebRTC and ORTC API.
|
||||||
|
/// Some of these functions are also exported globally using the
|
||||||
|
/// defaultAPI object. Note that the global version of the API
|
||||||
|
/// may be phased out in the future.
|
||||||
|
pub struct API {
|
||||||
|
pub(crate) setting_engine: Arc<SettingEngine>,
|
||||||
|
pub(crate) media_engine: Arc<MediaEngine>,
|
||||||
|
pub(crate) interceptor_registry: Registry,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl API {
|
||||||
|
/// new_peer_connection creates a new PeerConnection with the provided configuration against the received API object
|
||||||
|
pub async fn new_peer_connection(
|
||||||
|
&self,
|
||||||
|
configuration: RTCConfiguration,
|
||||||
|
) -> Result<RTCPeerConnection> {
|
||||||
|
RTCPeerConnection::new(self, configuration).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// new_ice_gatherer creates a new ice gatherer.
|
||||||
|
/// This constructor is part of the ORTC API. It is not
|
||||||
|
/// meant to be used together with the basic WebRTC API.
|
||||||
|
pub fn new_ice_gatherer(&self, opts: RTCIceGatherOptions) -> Result<RTCIceGatherer> {
|
||||||
|
let mut validated_servers = vec![];
|
||||||
|
if !opts.ice_servers.is_empty() {
|
||||||
|
for server in &opts.ice_servers {
|
||||||
|
let url = server.urls()?;
|
||||||
|
validated_servers.extend(url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(RTCIceGatherer::new(
|
||||||
|
validated_servers,
|
||||||
|
opts.ice_gather_policy,
|
||||||
|
Arc::clone(&self.setting_engine),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// new_ice_transport creates a new ice transport.
|
||||||
|
/// This constructor is part of the ORTC API. It is not
|
||||||
|
/// meant to be used together with the basic WebRTC API.
|
||||||
|
pub fn new_ice_transport(&self, gatherer: Arc<RTCIceGatherer>) -> RTCIceTransport {
|
||||||
|
RTCIceTransport::new(gatherer)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// new_dtls_transport creates a new dtls_transport transport.
|
||||||
|
/// This constructor is part of the ORTC API. It is not
|
||||||
|
/// meant to be used together with the basic WebRTC API.
|
||||||
|
pub fn new_dtls_transport(
|
||||||
|
&self,
|
||||||
|
ice_transport: Arc<RTCIceTransport>,
|
||||||
|
mut certificates: Vec<RTCCertificate>,
|
||||||
|
) -> Result<RTCDtlsTransport> {
|
||||||
|
if !certificates.is_empty() {
|
||||||
|
let now = SystemTime::now();
|
||||||
|
for cert in &certificates {
|
||||||
|
cert.expires
|
||||||
|
.duration_since(now)
|
||||||
|
.map_err(|_| Error::ErrCertificateExpired)?;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let kp = KeyPair::generate(&rcgen::PKCS_ECDSA_P256_SHA256)?;
|
||||||
|
let cert = RTCCertificate::from_key_pair(kp)?;
|
||||||
|
certificates = vec![cert];
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(RTCDtlsTransport::new(
|
||||||
|
ice_transport,
|
||||||
|
certificates,
|
||||||
|
Arc::clone(&self.setting_engine),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// new_sctp_transport creates a new SCTPTransport.
|
||||||
|
/// This constructor is part of the ORTC API. It is not
|
||||||
|
/// meant to be used together with the basic WebRTC API.
|
||||||
|
pub fn new_sctp_transport(
|
||||||
|
&self,
|
||||||
|
dtls_transport: Arc<RTCDtlsTransport>,
|
||||||
|
) -> Result<RTCSctpTransport> {
|
||||||
|
Ok(RTCSctpTransport::new(
|
||||||
|
dtls_transport,
|
||||||
|
Arc::clone(&self.setting_engine),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// new_data_channel creates a new DataChannel.
|
||||||
|
/// This constructor is part of the ORTC API. It is not
|
||||||
|
/// meant to be used together with the basic WebRTC API.
|
||||||
|
pub async fn new_data_channel(
|
||||||
|
&self,
|
||||||
|
sctp_transport: Arc<RTCSctpTransport>,
|
||||||
|
params: DataChannelParameters,
|
||||||
|
) -> Result<RTCDataChannel> {
|
||||||
|
// https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #5)
|
||||||
|
if params.label.len() > 65535 {
|
||||||
|
return Err(Error::ErrStringSizeLimit);
|
||||||
|
}
|
||||||
|
|
||||||
|
let d = RTCDataChannel::new(params, Arc::clone(&self.setting_engine));
|
||||||
|
d.open(sctp_transport).await?;
|
||||||
|
|
||||||
|
Ok(d)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// new_rtp_receiver constructs a new RTPReceiver
|
||||||
|
pub fn new_rtp_receiver(
|
||||||
|
&self,
|
||||||
|
kind: RTPCodecType,
|
||||||
|
transport: Arc<RTCDtlsTransport>,
|
||||||
|
interceptor: Arc<dyn Interceptor + Send + Sync>,
|
||||||
|
) -> RTCRtpReceiver {
|
||||||
|
RTCRtpReceiver::new(
|
||||||
|
self.setting_engine.get_receive_mtu(),
|
||||||
|
kind,
|
||||||
|
transport,
|
||||||
|
Arc::clone(&self.media_engine),
|
||||||
|
interceptor,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// new_rtp_sender constructs a new RTPSender
|
||||||
|
pub async fn new_rtp_sender(
|
||||||
|
&self,
|
||||||
|
track: Option<Arc<dyn TrackLocal + Send + Sync>>,
|
||||||
|
transport: Arc<RTCDtlsTransport>,
|
||||||
|
interceptor: Arc<dyn Interceptor + Send + Sync>,
|
||||||
|
) -> RTCRtpSender {
|
||||||
|
RTCRtpSender::new(
|
||||||
|
self.setting_engine.get_receive_mtu(),
|
||||||
|
track,
|
||||||
|
transport,
|
||||||
|
Arc::clone(&self.media_engine),
|
||||||
|
interceptor,
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the internal [`SettingEngine`].
|
||||||
|
pub fn setting_engine(&self) -> Arc<SettingEngine> {
|
||||||
|
Arc::clone(&self.setting_engine)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the internal [`MediaEngine`].
|
||||||
|
pub fn media_engine(&self) -> Arc<MediaEngine> {
|
||||||
|
Arc::clone(&self.media_engine)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct APIBuilder {
|
||||||
|
setting_engine: Option<Arc<SettingEngine>>,
|
||||||
|
media_engine: Option<Arc<MediaEngine>>,
|
||||||
|
interceptor_registry: Option<Registry>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl APIBuilder {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
APIBuilder::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build(mut self) -> API {
|
||||||
|
API {
|
||||||
|
setting_engine: if let Some(setting_engine) = self.setting_engine.take() {
|
||||||
|
setting_engine
|
||||||
|
} else {
|
||||||
|
Arc::new(SettingEngine::default())
|
||||||
|
},
|
||||||
|
media_engine: if let Some(media_engine) = self.media_engine.take() {
|
||||||
|
media_engine
|
||||||
|
} else {
|
||||||
|
Arc::new(MediaEngine::default())
|
||||||
|
},
|
||||||
|
interceptor_registry: if let Some(interceptor_registry) =
|
||||||
|
self.interceptor_registry.take()
|
||||||
|
{
|
||||||
|
interceptor_registry
|
||||||
|
} else {
|
||||||
|
Registry::new()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// WithSettingEngine allows providing a SettingEngine to the API.
|
||||||
|
/// Settings should not be changed after passing the engine to an API.
|
||||||
|
pub fn with_setting_engine(mut self, setting_engine: SettingEngine) -> Self {
|
||||||
|
self.setting_engine = Some(Arc::new(setting_engine));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// WithMediaEngine allows providing a MediaEngine to the API.
|
||||||
|
/// Settings can be changed after passing the engine to an API.
|
||||||
|
pub fn with_media_engine(mut self, media_engine: MediaEngine) -> Self {
|
||||||
|
self.media_engine = Some(Arc::new(media_engine));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// with_interceptor_registry allows providing Interceptors to the API.
|
||||||
|
/// Settings should not be changed after passing the registry to an API.
|
||||||
|
pub fn with_interceptor_registry(mut self, interceptor_registry: Registry) -> Self {
|
||||||
|
self.interceptor_registry = Some(interceptor_registry);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
+327
@@ -0,0 +1,327 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
mod setting_engine_test;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use dtls::extension::extension_use_srtp::SrtpProtectionProfile;
|
||||||
|
use ice::agent::agent_config::{InterfaceFilterFn, IpFilterFn};
|
||||||
|
use ice::mdns::MulticastDnsMode;
|
||||||
|
use ice::network_type::NetworkType;
|
||||||
|
use ice::udp_network::UDPNetwork;
|
||||||
|
use tokio::time::Duration;
|
||||||
|
use util::vnet::net::*;
|
||||||
|
|
||||||
|
use crate::dtls_transport::dtls_role::DTLSRole;
|
||||||
|
use crate::error::{Error, Result};
|
||||||
|
use crate::ice_transport::ice_candidate_type::RTCIceCandidateType;
|
||||||
|
use crate::RECEIVE_MTU;
|
||||||
|
|
||||||
|
#[derive(Default, Clone)]
|
||||||
|
pub struct Detach {
|
||||||
|
pub data_channels: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Clone)]
|
||||||
|
pub struct Timeout {
|
||||||
|
pub ice_disconnected_timeout: Option<Duration>,
|
||||||
|
pub ice_failed_timeout: Option<Duration>,
|
||||||
|
pub ice_keepalive_interval: Option<Duration>,
|
||||||
|
pub ice_host_acceptance_min_wait: Option<Duration>,
|
||||||
|
pub ice_srflx_acceptance_min_wait: Option<Duration>,
|
||||||
|
pub ice_prflx_acceptance_min_wait: Option<Duration>,
|
||||||
|
pub ice_relay_acceptance_min_wait: Option<Duration>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Clone)]
|
||||||
|
pub struct Candidates {
|
||||||
|
pub ice_lite: bool,
|
||||||
|
pub ice_network_types: Vec<NetworkType>,
|
||||||
|
pub interface_filter: Arc<Option<InterfaceFilterFn>>,
|
||||||
|
pub ip_filter: Arc<Option<IpFilterFn>>,
|
||||||
|
pub nat_1to1_ips: Vec<String>,
|
||||||
|
pub nat_1to1_ip_candidate_type: RTCIceCandidateType,
|
||||||
|
pub multicast_dns_mode: MulticastDnsMode,
|
||||||
|
pub multicast_dns_host_name: String,
|
||||||
|
pub username_fragment: String,
|
||||||
|
pub password: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Clone)]
|
||||||
|
pub struct ReplayProtection {
|
||||||
|
pub dtls: usize,
|
||||||
|
pub srtp: usize,
|
||||||
|
pub srtcp: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// SettingEngine allows influencing behavior in ways that are not
|
||||||
|
/// supported by the WebRTC API. This allows us to support additional
|
||||||
|
/// use-cases without deviating from the WebRTC API elsewhere.
|
||||||
|
#[derive(Default, Clone)]
|
||||||
|
pub struct SettingEngine {
|
||||||
|
pub(crate) detach: Detach,
|
||||||
|
pub(crate) timeout: Timeout,
|
||||||
|
pub(crate) candidates: Candidates,
|
||||||
|
pub(crate) replay_protection: ReplayProtection,
|
||||||
|
pub(crate) sdp_media_level_fingerprints: bool,
|
||||||
|
pub(crate) answering_dtls_role: DTLSRole,
|
||||||
|
pub(crate) disable_certificate_fingerprint_verification: bool,
|
||||||
|
pub(crate) allow_insecure_verification_algorithm: bool,
|
||||||
|
pub(crate) disable_srtp_replay_protection: bool,
|
||||||
|
pub(crate) disable_srtcp_replay_protection: bool,
|
||||||
|
pub(crate) vnet: Option<Arc<Net>>,
|
||||||
|
//BufferFactory :func(packetType packetio.BufferPacketType, ssrc uint32) io.ReadWriteCloser,
|
||||||
|
//iceTCPMux :ice.TCPMux,?
|
||||||
|
//iceProxyDialer :proxy.Dialer,?
|
||||||
|
pub(crate) udp_network: UDPNetwork,
|
||||||
|
pub(crate) disable_media_engine_copy: bool,
|
||||||
|
pub(crate) srtp_protection_profiles: Vec<SrtpProtectionProfile>,
|
||||||
|
pub(crate) receive_mtu: usize,
|
||||||
|
pub(crate) mid_generator: Option<Arc<dyn Fn(isize) -> String + Send + Sync>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SettingEngine {
|
||||||
|
/// get_receive_mtu returns the configured MTU. If SettingEngine's MTU is configured to 0 it returns the default
|
||||||
|
pub(crate) fn get_receive_mtu(&self) -> usize {
|
||||||
|
if self.receive_mtu != 0 {
|
||||||
|
self.receive_mtu
|
||||||
|
} else {
|
||||||
|
RECEIVE_MTU
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// detach_data_channels enables detaching data channels. When enabled
|
||||||
|
/// data channels have to be detached in the OnOpen callback using the
|
||||||
|
/// DataChannel.Detach method.
|
||||||
|
pub fn detach_data_channels(&mut self) {
|
||||||
|
self.detach.data_channels = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_srtp_protection_profiles allows the user to override the default srtp Protection Profiles
|
||||||
|
/// The default srtp protection profiles are provided by the function `defaultSrtpProtectionProfiles`
|
||||||
|
pub fn set_srtp_protection_profiles(&mut self, profiles: Vec<SrtpProtectionProfile>) {
|
||||||
|
self.srtp_protection_profiles = profiles
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_ice_timeouts sets the behavior around ICE Timeouts
|
||||||
|
/// * disconnected_timeout is the duration without network activity before a Agent is considered disconnected. Default is 5 Seconds
|
||||||
|
/// * failed_timeout is the duration without network activity before a Agent is considered failed after disconnected. Default is 25 Seconds
|
||||||
|
/// * keep_alive_interval is how often the ICE Agent sends extra traffic if there is no activity, if media is flowing no traffic will be sent. Default is 2 seconds
|
||||||
|
pub fn set_ice_timeouts(
|
||||||
|
&mut self,
|
||||||
|
disconnected_timeout: Option<Duration>,
|
||||||
|
failed_timeout: Option<Duration>,
|
||||||
|
keep_alive_interval: Option<Duration>,
|
||||||
|
) {
|
||||||
|
self.timeout.ice_disconnected_timeout = disconnected_timeout;
|
||||||
|
self.timeout.ice_failed_timeout = failed_timeout;
|
||||||
|
self.timeout.ice_keepalive_interval = keep_alive_interval;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_host_acceptance_min_wait sets the icehost_acceptance_min_wait
|
||||||
|
pub fn set_host_acceptance_min_wait(&mut self, t: Option<Duration>) {
|
||||||
|
self.timeout.ice_host_acceptance_min_wait = t;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_srflx_acceptance_min_wait sets the icesrflx_acceptance_min_wait
|
||||||
|
pub fn set_srflx_acceptance_min_wait(&mut self, t: Option<Duration>) {
|
||||||
|
self.timeout.ice_srflx_acceptance_min_wait = t;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_prflx_acceptance_min_wait sets the iceprflx_acceptance_min_wait
|
||||||
|
pub fn set_prflx_acceptance_min_wait(&mut self, t: Option<Duration>) {
|
||||||
|
self.timeout.ice_prflx_acceptance_min_wait = t;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_relay_acceptance_min_wait sets the icerelay_acceptance_min_wait
|
||||||
|
pub fn set_relay_acceptance_min_wait(&mut self, t: Option<Duration>) {
|
||||||
|
self.timeout.ice_relay_acceptance_min_wait = t;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_udp_network allows ICE traffic to come through Ephemeral or UDPMux.
|
||||||
|
/// UDPMux drastically simplifying deployments where ports will need to be opened/forwarded.
|
||||||
|
/// UDPMux should be started prior to creating PeerConnections.
|
||||||
|
pub fn set_udp_network(&mut self, udp_network: UDPNetwork) {
|
||||||
|
self.udp_network = udp_network;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_lite configures whether or not the ice agent should be a lite agent
|
||||||
|
pub fn set_lite(&mut self, lite: bool) {
|
||||||
|
self.candidates.ice_lite = lite;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_network_types configures what types of candidate networks are supported
|
||||||
|
/// during local and server reflexive gathering.
|
||||||
|
pub fn set_network_types(&mut self, candidate_types: Vec<NetworkType>) {
|
||||||
|
self.candidates.ice_network_types = candidate_types;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_interface_filter sets the filtering functions when gathering ICE candidates
|
||||||
|
/// This can be used to exclude certain network interfaces from ICE. Which may be
|
||||||
|
/// useful if you know a certain interface will never succeed, or if you wish to reduce
|
||||||
|
/// the amount of information you wish to expose to the remote peer
|
||||||
|
pub fn set_interface_filter(&mut self, filter: InterfaceFilterFn) {
|
||||||
|
self.candidates.interface_filter = Arc::new(Some(filter));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_ip_filter sets the filtering functions when gathering ICE candidates
|
||||||
|
/// This can be used to exclude certain ip from ICE. Which may be
|
||||||
|
/// useful if you know a certain ip will never succeed, or if you wish to reduce
|
||||||
|
/// the amount of information you wish to expose to the remote peer
|
||||||
|
pub fn set_ip_filter(&mut self, filter: IpFilterFn) {
|
||||||
|
self.candidates.ip_filter = Arc::new(Some(filter));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_nat_1to1_ips sets a list of external IP addresses of 1:1 (D)NAT
|
||||||
|
/// and a candidate type for which the external IP address is used.
|
||||||
|
/// This is useful when you are host a server using Pion on an AWS EC2 instance
|
||||||
|
/// which has a private address, behind a 1:1 DNAT with a public IP (e.g.
|
||||||
|
/// Elastic IP). In this case, you can give the public IP address so that
|
||||||
|
/// Pion will use the public IP address in its candidate instead of the private
|
||||||
|
/// IP address. The second argument, candidate_type, is used to tell Pion which
|
||||||
|
/// type of candidate should use the given public IP address.
|
||||||
|
/// Two types of candidates are supported:
|
||||||
|
///
|
||||||
|
/// ICECandidateTypeHost:
|
||||||
|
/// The public IP address will be used for the host candidate in the SDP.
|
||||||
|
/// ICECandidateTypeSrflx:
|
||||||
|
/// A server reflexive candidate with the given public IP address will be added
|
||||||
|
/// to the SDP.
|
||||||
|
///
|
||||||
|
/// Please note that if you choose ICECandidateTypeHost, then the private IP address
|
||||||
|
/// won't be advertised with the peer. Also, this option cannot be used along with mDNS.
|
||||||
|
///
|
||||||
|
/// If you choose ICECandidateTypeSrflx, it simply adds a server reflexive candidate
|
||||||
|
/// with the public IP. The host candidate is still available along with mDNS
|
||||||
|
/// capabilities unaffected. Also, you cannot give STUN server URL at the same time.
|
||||||
|
/// It will result in an error otherwise.
|
||||||
|
pub fn set_nat_1to1_ips(&mut self, ips: Vec<String>, candidate_type: RTCIceCandidateType) {
|
||||||
|
self.candidates.nat_1to1_ips = ips;
|
||||||
|
self.candidates.nat_1to1_ip_candidate_type = candidate_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_answering_dtls_role sets the dtls_transport role that is selected when offering
|
||||||
|
/// The dtls_transport role controls if the WebRTC Client as a client or server. This
|
||||||
|
/// may be useful when interacting with non-compliant clients or debugging issues.
|
||||||
|
///
|
||||||
|
/// DTLSRoleActive:
|
||||||
|
/// Act as dtls_transport Client, send the ClientHello and starts the handshake
|
||||||
|
/// DTLSRolePassive:
|
||||||
|
/// Act as dtls_transport Server, wait for ClientHello
|
||||||
|
pub fn set_answering_dtls_role(&mut self, role: DTLSRole) -> Result<()> {
|
||||||
|
if role != DTLSRole::Client && role != DTLSRole::Server {
|
||||||
|
return Err(Error::ErrSettingEngineSetAnsweringDTLSRole);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.answering_dtls_role = role;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_vnet sets the VNet instance that is passed to ice
|
||||||
|
/// VNet is a virtual network layer, allowing users to simulate
|
||||||
|
/// different topologies, latency, loss and jitter. This can be useful for
|
||||||
|
/// learning WebRTC concepts or testing your application in a lab environment
|
||||||
|
pub fn set_vnet(&mut self, vnet: Option<Arc<Net>>) {
|
||||||
|
self.vnet = vnet;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_ice_multicast_dns_mode controls if ice queries and generates mDNS ICE Candidates
|
||||||
|
pub fn set_ice_multicast_dns_mode(&mut self, multicast_dns_mode: ice::mdns::MulticastDnsMode) {
|
||||||
|
self.candidates.multicast_dns_mode = multicast_dns_mode
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_multicast_dns_host_name sets a static HostName to be used by ice instead of generating one on startup
|
||||||
|
/// This should only be used for a single PeerConnection. Having multiple PeerConnections with the same HostName will cause
|
||||||
|
/// undefined behavior
|
||||||
|
pub fn set_multicast_dns_host_name(&mut self, host_name: String) {
|
||||||
|
self.candidates.multicast_dns_host_name = host_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_ice_credentials sets a staic uFrag/uPwd to be used by ice
|
||||||
|
/// This is useful if you want to do signalless WebRTC session, or having a reproducible environment with static credentials
|
||||||
|
pub fn set_ice_credentials(&mut self, username_fragment: String, password: String) {
|
||||||
|
self.candidates.username_fragment = username_fragment;
|
||||||
|
self.candidates.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// disable_certificate_fingerprint_verification disables fingerprint verification after dtls_transport Handshake has finished
|
||||||
|
pub fn disable_certificate_fingerprint_verification(&mut self, is_disabled: bool) {
|
||||||
|
self.disable_certificate_fingerprint_verification = is_disabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// allow_insecure_verification_algorithm allows the usage of certain signature verification
|
||||||
|
/// algorithm that are known to be vulnerable or deprecated.
|
||||||
|
pub fn allow_insecure_verification_algorithm(&mut self, is_allowed: bool) {
|
||||||
|
self.allow_insecure_verification_algorithm = is_allowed;
|
||||||
|
}
|
||||||
|
/// set_dtls_replay_protection_window sets a replay attack protection window size of dtls_transport connection.
|
||||||
|
pub fn set_dtls_replay_protection_window(&mut self, n: usize) {
|
||||||
|
self.replay_protection.dtls = n;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_srtp_replay_protection_window sets a replay attack protection window size of srtp session.
|
||||||
|
pub fn set_srtp_replay_protection_window(&mut self, n: usize) {
|
||||||
|
self.disable_srtp_replay_protection = false;
|
||||||
|
self.replay_protection.srtp = n;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_srtcp_replay_protection_window sets a replay attack protection window size of srtcp session.
|
||||||
|
pub fn set_srtcp_replay_protection_window(&mut self, n: usize) {
|
||||||
|
self.disable_srtcp_replay_protection = false;
|
||||||
|
self.replay_protection.srtcp = n;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// disable_srtp_replay_protection disables srtp replay protection.
|
||||||
|
pub fn disable_srtp_replay_protection(&mut self, is_disabled: bool) {
|
||||||
|
self.disable_srtp_replay_protection = is_disabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// disable_srtcp_replay_protection disables srtcp replay protection.
|
||||||
|
pub fn disable_srtcp_replay_protection(&mut self, is_disabled: bool) {
|
||||||
|
self.disable_srtcp_replay_protection = is_disabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_sdp_media_level_fingerprints configures the logic for dtls_transport Fingerprint insertion
|
||||||
|
/// If true, fingerprints will be inserted in the sdp at the fingerprint
|
||||||
|
/// level, instead of the session level. This helps with compatibility with
|
||||||
|
/// some webrtc implementations.
|
||||||
|
pub fn set_sdp_media_level_fingerprints(&mut self, sdp_media_level_fingerprints: bool) {
|
||||||
|
self.sdp_media_level_fingerprints = sdp_media_level_fingerprints;
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetICETCPMux enables ICE-TCP when set to a non-nil value. Make sure that
|
||||||
|
// NetworkTypeTCP4 or NetworkTypeTCP6 is enabled as well.
|
||||||
|
//pub fn SetICETCPMux(&mut self, tcpMux ice.TCPMux) {
|
||||||
|
// self.iceTCPMux = tcpMux
|
||||||
|
//}
|
||||||
|
|
||||||
|
// SetICEProxyDialer sets the proxy dialer interface based on golang.org/x/net/proxy.
|
||||||
|
//pub fn SetICEProxyDialer(&mut self, d proxy.Dialer) {
|
||||||
|
// self.iceProxyDialer = d
|
||||||
|
//}
|
||||||
|
|
||||||
|
/// disable_media_engine_copy stops the MediaEngine from being copied. This allows a user to modify
|
||||||
|
/// the MediaEngine after the PeerConnection has been constructed. This is useful if you wish to
|
||||||
|
/// modify codecs after signaling. Make sure not to share MediaEngines between PeerConnections.
|
||||||
|
pub fn disable_media_engine_copy(&mut self, is_disabled: bool) {
|
||||||
|
self.disable_media_engine_copy = is_disabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_receive_mtu sets the size of read buffer that copies incoming packets. This is optional.
|
||||||
|
/// Leave this 0 for the default receive_mtu
|
||||||
|
pub fn set_receive_mtu(&mut self, receive_mtu: usize) {
|
||||||
|
self.receive_mtu = receive_mtu;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets a callback used to generate mid for transceivers created by this side of the RTCPeerconnection.
|
||||||
|
/// By having separate "naming schemes" for mids generated by either side of a connection, it's
|
||||||
|
/// possible to reduce complexity when handling SDP offers/answers clashing.
|
||||||
|
///
|
||||||
|
/// The `isize` argument is currently greatest seen _numeric_ mid. Since mids don't need to be numeric
|
||||||
|
/// this doesn't necessarily indicating anything.
|
||||||
|
///
|
||||||
|
/// Note that the spec says: All MID values MUST be generated in a fashion that does not leak user
|
||||||
|
/// information, e.g., randomly or using a per-PeerConnection counter, and SHOULD be 3 bytes or less,
|
||||||
|
/// to allow them to efficiently fit into the RTP header extension
|
||||||
|
pub fn set_mid_generator(&mut self, f: impl Fn(isize) -> String + Send + Sync + 'static) {
|
||||||
|
self.mid_generator = Some(Arc::new(f));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,271 @@
|
|||||||
|
use std::sync::atomic::Ordering;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use crate::api::media_engine::MediaEngine;
|
||||||
|
use crate::api::APIBuilder;
|
||||||
|
use crate::peer_connection::peer_connection_test::*;
|
||||||
|
use crate::rtp_transceiver::rtp_codec::RTPCodecType;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_set_connection_timeout() -> Result<()> {
|
||||||
|
let mut s = SettingEngine::default();
|
||||||
|
|
||||||
|
assert_eq!(s.timeout.ice_disconnected_timeout, None);
|
||||||
|
assert_eq!(s.timeout.ice_failed_timeout, None);
|
||||||
|
assert_eq!(s.timeout.ice_keepalive_interval, None);
|
||||||
|
|
||||||
|
s.set_ice_timeouts(
|
||||||
|
Some(Duration::from_secs(1)),
|
||||||
|
Some(Duration::from_secs(2)),
|
||||||
|
Some(Duration::from_secs(3)),
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
s.timeout.ice_disconnected_timeout,
|
||||||
|
Some(Duration::from_secs(1))
|
||||||
|
);
|
||||||
|
assert_eq!(s.timeout.ice_failed_timeout, Some(Duration::from_secs(2)));
|
||||||
|
assert_eq!(
|
||||||
|
s.timeout.ice_keepalive_interval,
|
||||||
|
Some(Duration::from_secs(3))
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_detach_data_channels() -> Result<()> {
|
||||||
|
let mut s = SettingEngine::default();
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
!s.detach.data_channels,
|
||||||
|
"SettingEngine defaults aren't as expected."
|
||||||
|
);
|
||||||
|
|
||||||
|
s.detach_data_channels();
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
s.detach.data_channels,
|
||||||
|
"Failed to enable detached data channels."
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_set_nat_1to1_ips() -> Result<()> {
|
||||||
|
let mut s = SettingEngine::default();
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
s.candidates.nat_1to1_ips.is_empty(),
|
||||||
|
"Invalid default value"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
s.candidates.nat_1to1_ip_candidate_type == RTCIceCandidateType::Unspecified,
|
||||||
|
"Invalid default value"
|
||||||
|
);
|
||||||
|
|
||||||
|
let ips = vec!["1.2.3.4".to_owned()];
|
||||||
|
let typ = RTCIceCandidateType::Host;
|
||||||
|
s.set_nat_1to1_ips(ips, typ);
|
||||||
|
assert!(
|
||||||
|
!(s.candidates.nat_1to1_ips.len() != 1 || s.candidates.nat_1to1_ips[0] != "1.2.3.4"),
|
||||||
|
"Failed to set NAT1To1IPs"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
s.candidates.nat_1to1_ip_candidate_type == typ,
|
||||||
|
"Failed to set NAT1To1IPCandidateType"
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_set_answering_dtls_role() -> Result<()> {
|
||||||
|
let mut s = SettingEngine::default();
|
||||||
|
assert!(
|
||||||
|
s.set_answering_dtls_role(DTLSRole::Auto).is_err(),
|
||||||
|
"SetAnsweringDTLSRole can only be called with DTLSRoleClient or DTLSRoleServer"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
s.set_answering_dtls_role(DTLSRole::Unspecified).is_err(),
|
||||||
|
"SetAnsweringDTLSRole can only be called with DTLSRoleClient or DTLSRoleServer"
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_set_replay_protection() -> Result<()> {
|
||||||
|
let mut s = SettingEngine::default();
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
!(s.replay_protection.dtls != 0
|
||||||
|
|| s.replay_protection.srtp != 0
|
||||||
|
|| s.replay_protection.srtcp != 0),
|
||||||
|
"SettingEngine defaults aren't as expected."
|
||||||
|
);
|
||||||
|
|
||||||
|
s.set_dtls_replay_protection_window(128);
|
||||||
|
s.set_srtp_replay_protection_window(64);
|
||||||
|
s.set_srtcp_replay_protection_window(32);
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
!(s.replay_protection.dtls == 0 || s.replay_protection.dtls != 128),
|
||||||
|
"Failed to set DTLS replay protection window"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!(s.replay_protection.srtp == 0 || s.replay_protection.srtp != 64),
|
||||||
|
"Failed to set SRTP replay protection window"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!(s.replay_protection.srtcp == 0 || s.replay_protection.srtcp != 32),
|
||||||
|
"Failed to set SRTCP replay protection window"
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/*TODO:#[test] fn test_setting_engine_set_ice_tcp_mux() ->Result<()> {
|
||||||
|
|
||||||
|
listener, err := net.ListenTCP("tcp", &net.TCPAddr{})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
_ = listener.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
tcpMux := NewICETCPMux(nil, listener, 8)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
_ = tcpMux.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
let mut s = SettingEngine::default();
|
||||||
|
settingEngine.SetICETCPMux(tcpMux)
|
||||||
|
|
||||||
|
assert.Equal(t, tcpMux, settingEngine.iceTCPMux)
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_setting_engine_set_disable_media_engine_copy() -> Result<()> {
|
||||||
|
//"Copy"
|
||||||
|
{
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_default_codecs()?;
|
||||||
|
let api = APIBuilder::new().with_media_engine(m).build();
|
||||||
|
|
||||||
|
let (mut offerer, mut answerer) = new_pair(&api).await?;
|
||||||
|
|
||||||
|
offerer
|
||||||
|
.add_transceiver_from_kind(RTPCodecType::Video, None)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
signal_pair(&mut offerer, &mut answerer).await?;
|
||||||
|
|
||||||
|
// Assert that the MediaEngine the user created isn't modified
|
||||||
|
assert!(!api.media_engine.negotiated_video.load(Ordering::SeqCst));
|
||||||
|
{
|
||||||
|
let negotiated_video_codecs = api.media_engine.negotiated_video_codecs.lock();
|
||||||
|
assert!(negotiated_video_codecs.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assert that the internal MediaEngine is modified
|
||||||
|
assert!(offerer
|
||||||
|
.internal
|
||||||
|
.media_engine
|
||||||
|
.negotiated_video
|
||||||
|
.load(Ordering::SeqCst));
|
||||||
|
{
|
||||||
|
let negotiated_video_codecs =
|
||||||
|
offerer.internal.media_engine.negotiated_video_codecs.lock();
|
||||||
|
assert!(!negotiated_video_codecs.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
close_pair_now(&offerer, &answerer).await;
|
||||||
|
|
||||||
|
let (new_offerer, new_answerer) = new_pair(&api).await?;
|
||||||
|
|
||||||
|
// Assert that the first internal MediaEngine hasn't been cleared
|
||||||
|
assert!(offerer
|
||||||
|
.internal
|
||||||
|
.media_engine
|
||||||
|
.negotiated_video
|
||||||
|
.load(Ordering::SeqCst));
|
||||||
|
{
|
||||||
|
let negotiated_video_codecs =
|
||||||
|
offerer.internal.media_engine.negotiated_video_codecs.lock();
|
||||||
|
assert!(!negotiated_video_codecs.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assert that the new internal MediaEngine isn't modified
|
||||||
|
assert!(!new_offerer
|
||||||
|
.internal
|
||||||
|
.media_engine
|
||||||
|
.negotiated_video
|
||||||
|
.load(Ordering::SeqCst));
|
||||||
|
{
|
||||||
|
let negotiated_video_codecs = new_offerer
|
||||||
|
.internal
|
||||||
|
.media_engine
|
||||||
|
.negotiated_video_codecs
|
||||||
|
.lock();
|
||||||
|
assert!(negotiated_video_codecs.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
close_pair_now(&new_offerer, &new_answerer).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
//"No Copy"
|
||||||
|
{
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_default_codecs()?;
|
||||||
|
|
||||||
|
let mut s = SettingEngine::default();
|
||||||
|
s.disable_media_engine_copy(true);
|
||||||
|
|
||||||
|
let api = APIBuilder::new()
|
||||||
|
.with_media_engine(m)
|
||||||
|
.with_setting_engine(s)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let (mut offerer, mut answerer) = new_pair(&api).await?;
|
||||||
|
|
||||||
|
offerer
|
||||||
|
.add_transceiver_from_kind(RTPCodecType::Video, None)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
signal_pair(&mut offerer, &mut answerer).await?;
|
||||||
|
|
||||||
|
// Assert that the user MediaEngine was modified, so no copy happened
|
||||||
|
assert!(api.media_engine.negotiated_video.load(Ordering::SeqCst));
|
||||||
|
{
|
||||||
|
let negotiated_video_codecs = api.media_engine.negotiated_video_codecs.lock();
|
||||||
|
assert!(!negotiated_video_codecs.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
close_pair_now(&offerer, &answerer).await;
|
||||||
|
|
||||||
|
let (offerer, answerer) = new_pair(&api).await?;
|
||||||
|
|
||||||
|
// Assert that the new internal MediaEngine was modified, so no copy happened
|
||||||
|
assert!(offerer
|
||||||
|
.internal
|
||||||
|
.media_engine
|
||||||
|
.negotiated_video
|
||||||
|
.load(Ordering::SeqCst));
|
||||||
|
{
|
||||||
|
let negotiated_video_codecs =
|
||||||
|
offerer.internal.media_engine.negotiated_video_codecs.lock();
|
||||||
|
assert!(!negotiated_video_codecs.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
close_pair_now(&offerer, &answerer).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
/// DataChannelConfig can be used to configure properties of the underlying
|
||||||
|
/// channel such as data reliability.
|
||||||
|
#[derive(Default, Debug, Clone)]
|
||||||
|
pub struct RTCDataChannelInit {
|
||||||
|
/// ordered indicates if data is allowed to be delivered out of order. The
|
||||||
|
/// default value of true, guarantees that data will be delivered in order.
|
||||||
|
pub ordered: Option<bool>,
|
||||||
|
|
||||||
|
/// max_packet_life_time limits the time (in milliseconds) during which the
|
||||||
|
/// channel will transmit or retransmit data if not acknowledged. This value
|
||||||
|
/// may be clamped if it exceeds the maximum value supported.
|
||||||
|
pub max_packet_life_time: Option<u16>,
|
||||||
|
|
||||||
|
/// max_retransmits limits the number of times a channel will retransmit data
|
||||||
|
/// if not successfully delivered. This value may be clamped if it exceeds
|
||||||
|
/// the maximum value supported.
|
||||||
|
pub max_retransmits: Option<u16>,
|
||||||
|
|
||||||
|
/// protocol describes the subprotocol name used for this channel.
|
||||||
|
pub protocol: Option<String>,
|
||||||
|
|
||||||
|
/// negotiated describes if the data channel is created by the local peer or
|
||||||
|
/// the remote peer. The default value of None tells the user agent to
|
||||||
|
/// announce the channel in-band and instruct the other peer to dispatch a
|
||||||
|
/// corresponding DataChannel. If set to Some(id), it is up to the application
|
||||||
|
/// to negotiate the channel and create an DataChannel with the same id
|
||||||
|
/// at the other peer.
|
||||||
|
pub negotiated: Option<u16>,
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
use bytes::Bytes;
|
||||||
|
|
||||||
|
/// DataChannelMessage represents a message received from the
|
||||||
|
/// data channel. IsString will be set to true if the incoming
|
||||||
|
/// message is of the string type. Otherwise the message is of
|
||||||
|
/// a binary type.
|
||||||
|
#[derive(Default, Debug, Clone)]
|
||||||
|
pub struct DataChannelMessage {
|
||||||
|
pub is_string: bool,
|
||||||
|
pub data: Bytes,
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// DataChannelParameters describes the configuration of the DataChannel.
|
||||||
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct DataChannelParameters {
|
||||||
|
pub label: String,
|
||||||
|
pub protocol: String,
|
||||||
|
pub ordered: bool,
|
||||||
|
pub max_packet_life_time: u16,
|
||||||
|
pub max_retransmits: u16,
|
||||||
|
pub negotiated: Option<u16>,
|
||||||
|
}
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
/// DataChannelState indicates the state of a data channel.
|
||||||
|
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Serialize)]
|
||||||
|
pub enum RTCDataChannelState {
|
||||||
|
#[serde(rename = "unspecified")]
|
||||||
|
#[default]
|
||||||
|
Unspecified = 0,
|
||||||
|
|
||||||
|
/// DataChannelStateConnecting indicates that the data channel is being
|
||||||
|
/// established. This is the initial state of DataChannel, whether created
|
||||||
|
/// with create_data_channel, or dispatched as a part of an DataChannelEvent.
|
||||||
|
#[serde(rename = "connecting")]
|
||||||
|
Connecting,
|
||||||
|
|
||||||
|
/// DataChannelStateOpen indicates that the underlying data transport is
|
||||||
|
/// established and communication is possible.
|
||||||
|
#[serde(rename = "open")]
|
||||||
|
Open,
|
||||||
|
|
||||||
|
/// DataChannelStateClosing indicates that the procedure to close down the
|
||||||
|
/// underlying data transport has started.
|
||||||
|
#[serde(rename = "closing")]
|
||||||
|
Closing,
|
||||||
|
|
||||||
|
/// DataChannelStateClosed indicates that the underlying data transport
|
||||||
|
/// has been closed or could not be established.
|
||||||
|
#[serde(rename = "closed")]
|
||||||
|
Closed,
|
||||||
|
}
|
||||||
|
|
||||||
|
const DATA_CHANNEL_STATE_CONNECTING_STR: &str = "connecting";
|
||||||
|
const DATA_CHANNEL_STATE_OPEN_STR: &str = "open";
|
||||||
|
const DATA_CHANNEL_STATE_CLOSING_STR: &str = "closing";
|
||||||
|
const DATA_CHANNEL_STATE_CLOSED_STR: &str = "closed";
|
||||||
|
|
||||||
|
impl From<u8> for RTCDataChannelState {
|
||||||
|
fn from(v: u8) -> Self {
|
||||||
|
match v {
|
||||||
|
1 => RTCDataChannelState::Connecting,
|
||||||
|
2 => RTCDataChannelState::Open,
|
||||||
|
3 => RTCDataChannelState::Closing,
|
||||||
|
4 => RTCDataChannelState::Closed,
|
||||||
|
_ => RTCDataChannelState::Unspecified,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&str> for RTCDataChannelState {
|
||||||
|
fn from(raw: &str) -> Self {
|
||||||
|
match raw {
|
||||||
|
DATA_CHANNEL_STATE_CONNECTING_STR => RTCDataChannelState::Connecting,
|
||||||
|
DATA_CHANNEL_STATE_OPEN_STR => RTCDataChannelState::Open,
|
||||||
|
DATA_CHANNEL_STATE_CLOSING_STR => RTCDataChannelState::Closing,
|
||||||
|
DATA_CHANNEL_STATE_CLOSED_STR => RTCDataChannelState::Closed,
|
||||||
|
_ => RTCDataChannelState::Unspecified,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for RTCDataChannelState {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
let s = match *self {
|
||||||
|
RTCDataChannelState::Connecting => DATA_CHANNEL_STATE_CONNECTING_STR,
|
||||||
|
RTCDataChannelState::Open => DATA_CHANNEL_STATE_OPEN_STR,
|
||||||
|
RTCDataChannelState::Closing => DATA_CHANNEL_STATE_CLOSING_STR,
|
||||||
|
RTCDataChannelState::Closed => DATA_CHANNEL_STATE_CLOSED_STR,
|
||||||
|
RTCDataChannelState::Unspecified => crate::UNSPECIFIED_STR,
|
||||||
|
};
|
||||||
|
write!(f, "{s}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_new_data_channel_state() {
|
||||||
|
let tests = vec![
|
||||||
|
(crate::UNSPECIFIED_STR, RTCDataChannelState::Unspecified),
|
||||||
|
("connecting", RTCDataChannelState::Connecting),
|
||||||
|
("open", RTCDataChannelState::Open),
|
||||||
|
("closing", RTCDataChannelState::Closing),
|
||||||
|
("closed", RTCDataChannelState::Closed),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (state_string, expected_state) in tests {
|
||||||
|
assert_eq!(
|
||||||
|
RTCDataChannelState::from(state_string),
|
||||||
|
expected_state,
|
||||||
|
"testCase: {expected_state}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_data_channel_state_string() {
|
||||||
|
let tests = vec![
|
||||||
|
(RTCDataChannelState::Unspecified, crate::UNSPECIFIED_STR),
|
||||||
|
(RTCDataChannelState::Connecting, "connecting"),
|
||||||
|
(RTCDataChannelState::Open, "open"),
|
||||||
|
(RTCDataChannelState::Closing, "closing"),
|
||||||
|
(RTCDataChannelState::Closed, "closed"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (state, expected_string) in tests {
|
||||||
|
assert_eq!(state.to_string(), expected_string)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+1504
File diff suppressed because it is too large.
Load diff
+558
@@ -0,0 +1,558 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
mod data_channel_test;
|
||||||
|
|
||||||
|
pub mod data_channel_init;
|
||||||
|
pub mod data_channel_message;
|
||||||
|
pub mod data_channel_parameters;
|
||||||
|
pub mod data_channel_state;
|
||||||
|
|
||||||
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::sync::atomic::{AtomicBool, AtomicU16, AtomicU8, AtomicUsize, Ordering};
|
||||||
|
use std::sync::{Arc, Weak};
|
||||||
|
use std::time::SystemTime;
|
||||||
|
|
||||||
|
use arc_swap::ArcSwapOption;
|
||||||
|
use bytes::Bytes;
|
||||||
|
use data::message::message_channel_open::ChannelType;
|
||||||
|
use data_channel_message::*;
|
||||||
|
use data_channel_parameters::*;
|
||||||
|
use data_channel_state::RTCDataChannelState;
|
||||||
|
use sctp::stream::OnBufferedAmountLowFn;
|
||||||
|
use tokio::sync::{Mutex, Notify};
|
||||||
|
use util::sync::Mutex as SyncMutex;
|
||||||
|
|
||||||
|
use crate::api::setting_engine::SettingEngine;
|
||||||
|
use crate::error::{Error, OnErrorHdlrFn, Result};
|
||||||
|
use crate::sctp_transport::RTCSctpTransport;
|
||||||
|
use crate::stats::stats_collector::StatsCollector;
|
||||||
|
use crate::stats::{DataChannelStats, StatsReportType};
|
||||||
|
|
||||||
|
/// message size limit for Chromium
|
||||||
|
// uu-remote-linux patch: receive messages up to the advertised a=max-message-size (libwebrtc's
|
||||||
|
// 262144 default). The upstream u16::MAX buffer breaks the channel on larger messages, and the
|
||||||
|
// official viewer sends port-mapping frames bigger than 64 KB.
|
||||||
|
const DATA_CHANNEL_BUFFER_SIZE: usize = 262_144;
|
||||||
|
|
||||||
|
pub type OnMessageHdlrFn = Box<
|
||||||
|
dyn (FnMut(DataChannelMessage) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>)
|
||||||
|
+ Send
|
||||||
|
+ Sync,
|
||||||
|
>;
|
||||||
|
|
||||||
|
pub type OnOpenHdlrFn =
|
||||||
|
Box<dyn (FnOnce() -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>) + Send + Sync>;
|
||||||
|
|
||||||
|
pub type OnCloseHdlrFn =
|
||||||
|
Box<dyn (FnMut() -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>) + Send + Sync>;
|
||||||
|
|
||||||
|
/// DataChannel represents a WebRTC DataChannel
|
||||||
|
/// The DataChannel interface represents a network channel
|
||||||
|
/// which can be used for bidirectional peer-to-peer transfers of arbitrary data
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct RTCDataChannel {
|
||||||
|
pub(crate) stats_id: String,
|
||||||
|
pub(crate) label: String,
|
||||||
|
pub(crate) ordered: bool,
|
||||||
|
pub(crate) max_packet_lifetime: u16,
|
||||||
|
pub(crate) max_retransmits: u16,
|
||||||
|
pub(crate) protocol: String,
|
||||||
|
pub(crate) negotiated: bool,
|
||||||
|
pub(crate) id: AtomicU16,
|
||||||
|
pub(crate) ready_state: Arc<AtomicU8>, // DataChannelState
|
||||||
|
pub(crate) buffered_amount_low_threshold: AtomicUsize,
|
||||||
|
pub(crate) detach_called: Arc<AtomicBool>,
|
||||||
|
|
||||||
|
// The binaryType represents attribute MUST, on getting, return the value to
|
||||||
|
// which it was last set. On setting, if the new value is either the string
|
||||||
|
// "blob" or the string "arraybuffer", then set the IDL attribute to this
|
||||||
|
// new value. Otherwise, throw a SyntaxError. When an DataChannel object
|
||||||
|
// is created, the binaryType attribute MUST be initialized to the string
|
||||||
|
// "blob". This attribute controls how binary data is exposed to scripts.
|
||||||
|
// binaryType string
|
||||||
|
pub(crate) on_message_handler: Arc<ArcSwapOption<Mutex<OnMessageHdlrFn>>>,
|
||||||
|
pub(crate) on_open_handler: SyncMutex<Option<OnOpenHdlrFn>>,
|
||||||
|
pub(crate) on_close_handler: Arc<ArcSwapOption<Mutex<OnCloseHdlrFn>>>,
|
||||||
|
pub(crate) on_error_handler: Arc<ArcSwapOption<Mutex<OnErrorHdlrFn>>>,
|
||||||
|
|
||||||
|
pub(crate) on_buffered_amount_low: Mutex<Option<OnBufferedAmountLowFn>>,
|
||||||
|
|
||||||
|
pub(crate) sctp_transport: Mutex<Option<Weak<RTCSctpTransport>>>,
|
||||||
|
pub(crate) data_channel: Mutex<Option<Arc<data::data_channel::DataChannel>>>,
|
||||||
|
|
||||||
|
pub(crate) notify_tx: Arc<Notify>,
|
||||||
|
|
||||||
|
// A reference to the associated api object used by this datachannel
|
||||||
|
pub(crate) setting_engine: Arc<SettingEngine>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RTCDataChannel {
|
||||||
|
// create the DataChannel object before the networking is set up.
|
||||||
|
pub(crate) fn new(params: DataChannelParameters, setting_engine: Arc<SettingEngine>) -> Self {
|
||||||
|
// the id value if non-negotiated doesn't matter, since it will be overwritten
|
||||||
|
// on opening
|
||||||
|
let id = params.negotiated.unwrap_or(0);
|
||||||
|
RTCDataChannel {
|
||||||
|
stats_id: format!(
|
||||||
|
"DataChannel-{}",
|
||||||
|
SystemTime::now()
|
||||||
|
.duration_since(SystemTime::UNIX_EPOCH)
|
||||||
|
.map_or(0, |d| d.as_nanos())
|
||||||
|
),
|
||||||
|
label: params.label,
|
||||||
|
protocol: params.protocol,
|
||||||
|
negotiated: params.negotiated.is_some(),
|
||||||
|
id: AtomicU16::new(id),
|
||||||
|
ordered: params.ordered,
|
||||||
|
max_packet_lifetime: params.max_packet_life_time,
|
||||||
|
max_retransmits: params.max_retransmits,
|
||||||
|
ready_state: Arc::new(AtomicU8::new(RTCDataChannelState::Connecting as u8)),
|
||||||
|
detach_called: Arc::new(AtomicBool::new(false)),
|
||||||
|
|
||||||
|
notify_tx: Arc::new(Notify::new()),
|
||||||
|
|
||||||
|
setting_engine,
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// open opens the datachannel over the sctp transport
|
||||||
|
pub(crate) async fn open(&self, sctp_transport: Arc<RTCSctpTransport>) -> Result<()> {
|
||||||
|
if let Some(association) = sctp_transport.association().await {
|
||||||
|
{
|
||||||
|
let mut st = self.sctp_transport.lock().await;
|
||||||
|
if st.is_none() {
|
||||||
|
*st = Some(Arc::downgrade(&sctp_transport));
|
||||||
|
} else {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let channel_type;
|
||||||
|
let reliability_parameter;
|
||||||
|
|
||||||
|
if self.max_packet_lifetime == 0 && self.max_retransmits == 0 {
|
||||||
|
reliability_parameter = 0u32;
|
||||||
|
if self.ordered {
|
||||||
|
channel_type = ChannelType::Reliable;
|
||||||
|
} else {
|
||||||
|
channel_type = ChannelType::ReliableUnordered;
|
||||||
|
}
|
||||||
|
} else if self.max_retransmits != 0 {
|
||||||
|
reliability_parameter = self.max_retransmits as u32;
|
||||||
|
if self.ordered {
|
||||||
|
channel_type = ChannelType::PartialReliableRexmit;
|
||||||
|
} else {
|
||||||
|
channel_type = ChannelType::PartialReliableRexmitUnordered;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
reliability_parameter = self.max_packet_lifetime as u32;
|
||||||
|
if self.ordered {
|
||||||
|
channel_type = ChannelType::PartialReliableTimed;
|
||||||
|
} else {
|
||||||
|
channel_type = ChannelType::PartialReliableTimedUnordered;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let cfg = data::data_channel::Config {
|
||||||
|
channel_type,
|
||||||
|
priority: data::message::message_channel_open::CHANNEL_PRIORITY_NORMAL,
|
||||||
|
reliability_parameter,
|
||||||
|
label: self.label.clone(),
|
||||||
|
protocol: self.protocol.clone(),
|
||||||
|
negotiated: self.negotiated,
|
||||||
|
};
|
||||||
|
|
||||||
|
if !self.negotiated {
|
||||||
|
self.id.store(
|
||||||
|
sctp_transport
|
||||||
|
.generate_and_set_data_channel_id(
|
||||||
|
sctp_transport.dtls_transport.role().await,
|
||||||
|
)
|
||||||
|
.await?,
|
||||||
|
Ordering::SeqCst,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let dc = data::data_channel::DataChannel::dial(&association, self.id(), cfg).await?;
|
||||||
|
|
||||||
|
// buffered_amount_low_threshold and on_buffered_amount_low might be set earlier
|
||||||
|
dc.set_buffered_amount_low_threshold(
|
||||||
|
self.buffered_amount_low_threshold.load(Ordering::SeqCst),
|
||||||
|
);
|
||||||
|
{
|
||||||
|
let mut on_buffered_amount_low = self.on_buffered_amount_low.lock().await;
|
||||||
|
if let Some(f) = on_buffered_amount_low.take() {
|
||||||
|
dc.on_buffered_amount_low(f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.handle_open(Arc::new(dc)).await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(Error::ErrSCTPNotEstablished)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// transport returns the SCTPTransport instance the DataChannel is sending over.
|
||||||
|
pub async fn transport(&self) -> Option<Weak<RTCSctpTransport>> {
|
||||||
|
let sctp_transport = self.sctp_transport.lock().await;
|
||||||
|
sctp_transport.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// on_open sets an event handler which is invoked when
|
||||||
|
/// the underlying data transport has been established (or re-established).
|
||||||
|
pub fn on_open(&self, f: OnOpenHdlrFn) {
|
||||||
|
let _ = self.on_open_handler.lock().replace(f);
|
||||||
|
|
||||||
|
if self.ready_state() == RTCDataChannelState::Open {
|
||||||
|
self.do_open();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn do_open(&self) {
|
||||||
|
let on_open_handler = self.on_open_handler.lock().take();
|
||||||
|
if on_open_handler.is_none() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let detach_data_channels = self.setting_engine.detach.data_channels;
|
||||||
|
let detach_called = Arc::clone(&self.detach_called);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
if let Some(f) = on_open_handler {
|
||||||
|
f().await;
|
||||||
|
|
||||||
|
// self.check_detach_after_open();
|
||||||
|
// After onOpen is complete check that the user called detach
|
||||||
|
// and provide an error message if the call was missed
|
||||||
|
if detach_data_channels && !detach_called.load(Ordering::SeqCst) {
|
||||||
|
log::warn!(
|
||||||
|
"webrtc.DetachDataChannels() enabled but didn't Detach, call Detach from OnOpen"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// on_close sets an event handler which is invoked when
|
||||||
|
/// the underlying data transport has been closed.
|
||||||
|
pub fn on_close(&self, f: OnCloseHdlrFn) {
|
||||||
|
self.on_close_handler.store(Some(Arc::new(Mutex::new(f))));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// on_message sets an event handler which is invoked on a binary
|
||||||
|
/// message arrival over the sctp transport from a remote peer.
|
||||||
|
/// OnMessage can currently receive messages up to 16384 bytes
|
||||||
|
/// in size. Check out the detach API if you want to use larger
|
||||||
|
/// message sizes. Note that browser support for larger messages
|
||||||
|
/// is also limited.
|
||||||
|
pub fn on_message(&self, f: OnMessageHdlrFn) {
|
||||||
|
self.on_message_handler.store(Some(Arc::new(Mutex::new(f))));
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn do_message(&self, msg: DataChannelMessage) {
|
||||||
|
if let Some(handler) = &*self.on_message_handler.load() {
|
||||||
|
let mut f = handler.lock().await;
|
||||||
|
f(msg).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn handle_open(&self, dc: Arc<data::data_channel::DataChannel>) {
|
||||||
|
{
|
||||||
|
let mut data_channel = self.data_channel.lock().await;
|
||||||
|
*data_channel = Some(Arc::clone(&dc));
|
||||||
|
}
|
||||||
|
self.set_ready_state(RTCDataChannelState::Open);
|
||||||
|
|
||||||
|
self.do_open();
|
||||||
|
|
||||||
|
if !self.setting_engine.detach.data_channels {
|
||||||
|
let ready_state = Arc::clone(&self.ready_state);
|
||||||
|
let on_message_handler = Arc::clone(&self.on_message_handler);
|
||||||
|
let on_close_handler = Arc::clone(&self.on_close_handler);
|
||||||
|
let on_error_handler = Arc::clone(&self.on_error_handler);
|
||||||
|
let notify_rx = self.notify_tx.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
RTCDataChannel::read_loop(
|
||||||
|
notify_rx,
|
||||||
|
dc,
|
||||||
|
ready_state,
|
||||||
|
on_message_handler,
|
||||||
|
on_close_handler,
|
||||||
|
on_error_handler,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// on_error sets an event handler which is invoked when
|
||||||
|
/// the underlying data transport cannot be read.
|
||||||
|
pub fn on_error(&self, f: OnErrorHdlrFn) {
|
||||||
|
self.on_error_handler.store(Some(Arc::new(Mutex::new(f))));
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read_loop(
|
||||||
|
notify_rx: Arc<Notify>,
|
||||||
|
data_channel: Arc<data::data_channel::DataChannel>,
|
||||||
|
ready_state: Arc<AtomicU8>,
|
||||||
|
on_message_handler: Arc<ArcSwapOption<Mutex<OnMessageHdlrFn>>>,
|
||||||
|
on_close_handler: Arc<ArcSwapOption<Mutex<OnCloseHdlrFn>>>,
|
||||||
|
on_error_handler: Arc<ArcSwapOption<Mutex<OnErrorHdlrFn>>>,
|
||||||
|
) {
|
||||||
|
let mut buffer = vec![0u8; DATA_CHANNEL_BUFFER_SIZE];
|
||||||
|
loop {
|
||||||
|
let (n, is_string) = tokio::select! {
|
||||||
|
_ = notify_rx.notified() => break,
|
||||||
|
result = data_channel.read_data_channel(&mut buffer) => {
|
||||||
|
match result{
|
||||||
|
// EOF (`data_channel` was either closed or the underlying stream got
|
||||||
|
// reset by the remote) => close and run `on_close` handler.
|
||||||
|
Ok((0, _)) =>
|
||||||
|
{
|
||||||
|
ready_state.store(RTCDataChannelState::Closed as u8, Ordering::SeqCst);
|
||||||
|
|
||||||
|
let on_close_handler2 = Arc::clone(&on_close_handler);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
if let Some(handler) = &*on_close_handler2.load() {
|
||||||
|
let mut f = handler.lock().await;
|
||||||
|
f().await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Ok((n, is_string)) => (n, is_string),
|
||||||
|
Err(err) => {
|
||||||
|
ready_state.store(RTCDataChannelState::Closed as u8, Ordering::SeqCst);
|
||||||
|
|
||||||
|
let on_error_handler2 = Arc::clone(&on_error_handler);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
if let Some(handler) = &*on_error_handler2.load() {
|
||||||
|
let mut f = handler.lock().await;
|
||||||
|
f(err.into()).await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let on_close_handler2 = Arc::clone(&on_close_handler);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
if let Some(handler) = &*on_close_handler2.load() {
|
||||||
|
let mut f = handler.lock().await;
|
||||||
|
f().await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(handler) = &*on_message_handler.load() {
|
||||||
|
let mut f = handler.lock().await;
|
||||||
|
f(DataChannelMessage {
|
||||||
|
is_string,
|
||||||
|
data: Bytes::from(buffer[..n].to_vec()),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// send sends the binary message to the DataChannel peer
|
||||||
|
pub async fn send(&self, data: &Bytes) -> Result<usize> {
|
||||||
|
self.ensure_open()?;
|
||||||
|
|
||||||
|
let data_channel = self.data_channel.lock().await;
|
||||||
|
if let Some(dc) = &*data_channel {
|
||||||
|
Ok(dc.write_data_channel(data, false).await?)
|
||||||
|
} else {
|
||||||
|
Err(Error::ErrClosedPipe)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// send_text sends the text message to the DataChannel peer
|
||||||
|
pub async fn send_text(&self, s: impl Into<String>) -> Result<usize> {
|
||||||
|
self.ensure_open()?;
|
||||||
|
|
||||||
|
let data_channel = self.data_channel.lock().await;
|
||||||
|
if let Some(dc) = &*data_channel {
|
||||||
|
Ok(dc.write_data_channel(&Bytes::from(s.into()), true).await?)
|
||||||
|
} else {
|
||||||
|
Err(Error::ErrClosedPipe)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ensure_open(&self) -> Result<()> {
|
||||||
|
if self.ready_state() != RTCDataChannelState::Open {
|
||||||
|
Err(Error::ErrClosedPipe)
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// detach allows you to detach the underlying datachannel. This provides
|
||||||
|
/// an idiomatic API to work with, however it disables the OnMessage callback.
|
||||||
|
/// Before calling Detach you have to enable this behavior by calling
|
||||||
|
/// webrtc.DetachDataChannels(). Combining detached and normal data channels
|
||||||
|
/// is not supported.
|
||||||
|
/// Please refer to the data-channels-detach example and the
|
||||||
|
/// pion/datachannel documentation for the correct way to handle the
|
||||||
|
/// resulting DataChannel object.
|
||||||
|
pub async fn detach(&self) -> Result<Arc<data::data_channel::DataChannel>> {
|
||||||
|
if !self.setting_engine.detach.data_channels {
|
||||||
|
return Err(Error::ErrDetachNotEnabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
let data_channel = self.data_channel.lock().await;
|
||||||
|
if let Some(dc) = &*data_channel {
|
||||||
|
self.detach_called.store(true, Ordering::SeqCst);
|
||||||
|
|
||||||
|
Ok(Arc::clone(dc))
|
||||||
|
} else {
|
||||||
|
Err(Error::ErrDetachBeforeOpened)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Close Closes the DataChannel. It may be called regardless of whether
|
||||||
|
/// the DataChannel object was created by this peer or the remote peer.
|
||||||
|
pub async fn close(&self) -> Result<()> {
|
||||||
|
if self.ready_state() == RTCDataChannelState::Closed {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
self.set_ready_state(RTCDataChannelState::Closing);
|
||||||
|
self.notify_tx.notify_waiters();
|
||||||
|
|
||||||
|
let data_channel = self.data_channel.lock().await;
|
||||||
|
if let Some(dc) = &*data_channel {
|
||||||
|
Ok(dc.close().await?)
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// label represents a label that can be used to distinguish this
|
||||||
|
/// DataChannel object from other DataChannel objects. Scripts are
|
||||||
|
/// allowed to create multiple DataChannel objects with the same label.
|
||||||
|
pub fn label(&self) -> &str {
|
||||||
|
self.label.as_str()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ordered returns true if the DataChannel is ordered, and false if
|
||||||
|
/// out-of-order delivery is allowed.
|
||||||
|
pub fn ordered(&self) -> bool {
|
||||||
|
self.ordered
|
||||||
|
}
|
||||||
|
|
||||||
|
/// max_packet_lifetime represents the length of the time window (msec) during
|
||||||
|
/// which transmissions and retransmissions may occur in unreliable mode.
|
||||||
|
pub fn max_packet_lifetime(&self) -> u16 {
|
||||||
|
self.max_packet_lifetime
|
||||||
|
}
|
||||||
|
|
||||||
|
/// max_retransmits represents the maximum number of retransmissions that are
|
||||||
|
/// attempted in unreliable mode.
|
||||||
|
pub fn max_retransmits(&self) -> u16 {
|
||||||
|
self.max_retransmits
|
||||||
|
}
|
||||||
|
|
||||||
|
/// protocol represents the name of the sub-protocol used with this
|
||||||
|
/// DataChannel.
|
||||||
|
pub fn protocol(&self) -> &str {
|
||||||
|
self.protocol.as_str()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// negotiated represents whether this DataChannel was negotiated by the
|
||||||
|
/// application (true), or not (false).
|
||||||
|
pub fn negotiated(&self) -> bool {
|
||||||
|
self.negotiated
|
||||||
|
}
|
||||||
|
|
||||||
|
/// ID represents the ID for this DataChannel. The value is initially
|
||||||
|
/// null, which is what will be returned if the ID was not provided at
|
||||||
|
/// channel creation time, and the DTLS role of the SCTP transport has not
|
||||||
|
/// yet been negotiated. Otherwise, it will return the ID that was either
|
||||||
|
/// selected by the script or generated. After the ID is set to a non-null
|
||||||
|
/// value, it will not change.
|
||||||
|
pub fn id(&self) -> u16 {
|
||||||
|
self.id.load(Ordering::SeqCst)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// ready_state represents the state of the DataChannel object.
|
||||||
|
pub fn ready_state(&self) -> RTCDataChannelState {
|
||||||
|
self.ready_state.load(Ordering::SeqCst).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// buffered_amount represents the number of bytes of application data
|
||||||
|
/// (UTF-8 text and binary data) that have been queued using send(). Even
|
||||||
|
/// though the data transmission can occur in parallel, the returned value
|
||||||
|
/// MUST NOT be decreased before the current task yielded back to the event
|
||||||
|
/// loop to prevent race conditions. The value does not include framing
|
||||||
|
/// overhead incurred by the protocol, or buffering done by the operating
|
||||||
|
/// system or network hardware. The value of buffered_amount slot will only
|
||||||
|
/// increase with each call to the send() method as long as the ready_state is
|
||||||
|
/// open; however, buffered_amount does not reset to zero once the channel
|
||||||
|
/// closes.
|
||||||
|
pub async fn buffered_amount(&self) -> usize {
|
||||||
|
let data_channel = self.data_channel.lock().await;
|
||||||
|
if let Some(dc) = &*data_channel {
|
||||||
|
dc.buffered_amount()
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// buffered_amount_low_threshold represents the threshold at which the
|
||||||
|
/// bufferedAmount is considered to be low. When the bufferedAmount decreases
|
||||||
|
/// from above this threshold to equal or below it, the bufferedamountlow
|
||||||
|
/// event fires. buffered_amount_low_threshold is initially zero on each new
|
||||||
|
/// DataChannel, but the application may change its value at any time.
|
||||||
|
/// The threshold is set to 0 by default.
|
||||||
|
pub async fn buffered_amount_low_threshold(&self) -> usize {
|
||||||
|
let data_channel = self.data_channel.lock().await;
|
||||||
|
if let Some(dc) = &*data_channel {
|
||||||
|
dc.buffered_amount_low_threshold()
|
||||||
|
} else {
|
||||||
|
self.buffered_amount_low_threshold.load(Ordering::SeqCst)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set_buffered_amount_low_threshold is used to update the threshold.
|
||||||
|
/// See buffered_amount_low_threshold().
|
||||||
|
pub async fn set_buffered_amount_low_threshold(&self, th: usize) {
|
||||||
|
self.buffered_amount_low_threshold
|
||||||
|
.store(th, Ordering::SeqCst);
|
||||||
|
let data_channel = self.data_channel.lock().await;
|
||||||
|
if let Some(dc) = &*data_channel {
|
||||||
|
dc.set_buffered_amount_low_threshold(th);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// on_buffered_amount_low sets an event handler which is invoked when
|
||||||
|
/// the number of bytes of outgoing data becomes lower than the
|
||||||
|
/// buffered_amount_low_threshold.
|
||||||
|
pub async fn on_buffered_amount_low(&self, f: OnBufferedAmountLowFn) {
|
||||||
|
let data_channel = self.data_channel.lock().await;
|
||||||
|
if let Some(dc) = &*data_channel {
|
||||||
|
dc.on_buffered_amount_low(f);
|
||||||
|
} else {
|
||||||
|
let mut on_buffered_amount_low = self.on_buffered_amount_low.lock().await;
|
||||||
|
*on_buffered_amount_low = Some(f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn get_stats_id(&self) -> &str {
|
||||||
|
self.stats_id.as_str()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn collect_stats(&self, collector: &StatsCollector) {
|
||||||
|
let stats = DataChannelStats::from(self).await;
|
||||||
|
collector.insert(self.stats_id.clone(), StatsReportType::DataChannel(stats));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn set_ready_state(&self, r: RTCDataChannelState) {
|
||||||
|
self.ready_state.store(r as u8, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// DTLSFingerprint specifies the hash function algorithm and certificate
|
||||||
|
/// fingerprint as described in <https://tools.ietf.org/html/rfc4572>.
|
||||||
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct RTCDtlsFingerprint {
|
||||||
|
/// Algorithm specifies one of the the hash function algorithms defined in
|
||||||
|
/// the 'Hash function Textual Names' registry.
|
||||||
|
pub algorithm: String,
|
||||||
|
|
||||||
|
/// Value specifies the value of the certificate fingerprint in lowercase
|
||||||
|
/// hex string as expressed utilizing the syntax of 'fingerprint' in
|
||||||
|
/// <https://tools.ietf.org/html/rfc4572#section-5>.
|
||||||
|
pub value: String,
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use super::dtls_fingerprint::*;
|
||||||
|
use super::dtls_role::*;
|
||||||
|
|
||||||
|
/// DTLSParameters holds information relating to DTLS configuration.
|
||||||
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct DTLSParameters {
|
||||||
|
pub role: DTLSRole,
|
||||||
|
pub fingerprints: Vec<RTCDtlsFingerprint>,
|
||||||
|
}
|
||||||
+170
@@ -0,0 +1,170 @@
|
|||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
use sdp::description::session::SessionDescription;
|
||||||
|
use sdp::util::ConnectionRole;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// DtlsRole indicates the role of the DTLS transport.
|
||||||
|
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
pub enum DTLSRole {
|
||||||
|
#[default]
|
||||||
|
Unspecified = 0,
|
||||||
|
|
||||||
|
/// DTLSRoleAuto defines the DTLS role is determined based on
|
||||||
|
/// the resolved ICE role: the ICE controlled role acts as the DTLS
|
||||||
|
/// client and the ICE controlling role acts as the DTLS server.
|
||||||
|
#[serde(rename = "auto")]
|
||||||
|
Auto = 1,
|
||||||
|
|
||||||
|
/// DTLSRoleClient defines the DTLS client role.
|
||||||
|
#[serde(rename = "client")]
|
||||||
|
Client = 2,
|
||||||
|
|
||||||
|
/// DTLSRoleServer defines the DTLS server role.
|
||||||
|
#[serde(rename = "server")]
|
||||||
|
Server = 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <https://tools.ietf.org/html/rfc5763>
|
||||||
|
/// The answerer MUST use either a
|
||||||
|
/// setup attribute value of setup:active or setup:passive. Note that
|
||||||
|
/// if the answerer uses setup:passive, then the DTLS handshake will
|
||||||
|
/// not begin until the answerer is received, which adds additional
|
||||||
|
/// latency. setup:active allows the answer and the DTLS handshake to
|
||||||
|
/// occur in parallel. Thus, setup:active is RECOMMENDED.
|
||||||
|
pub(crate) const DEFAULT_DTLS_ROLE_ANSWER: DTLSRole = DTLSRole::Client;
|
||||||
|
|
||||||
|
/// The endpoint that is the offerer MUST use the setup attribute
|
||||||
|
/// value of setup:actpass and be prepared to receive a client_hello
|
||||||
|
/// before it receives the answer.
|
||||||
|
pub(crate) const DEFAULT_DTLS_ROLE_OFFER: DTLSRole = DTLSRole::Auto;
|
||||||
|
|
||||||
|
impl fmt::Display for DTLSRole {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match *self {
|
||||||
|
DTLSRole::Auto => write!(f, "auto"),
|
||||||
|
DTLSRole::Client => write!(f, "client"),
|
||||||
|
DTLSRole::Server => write!(f, "server"),
|
||||||
|
_ => write!(f, "{}", crate::UNSPECIFIED_STR),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Iterate a SessionDescription from a remote to determine if an explicit
|
||||||
|
/// role can been determined from it. The decision is made from the first role we we parse.
|
||||||
|
/// If no role can be found we return DTLSRoleAuto
|
||||||
|
impl From<&SessionDescription> for DTLSRole {
|
||||||
|
fn from(session_description: &SessionDescription) -> Self {
|
||||||
|
for media_section in &session_description.media_descriptions {
|
||||||
|
for attribute in &media_section.attributes {
|
||||||
|
if attribute.key == "setup" {
|
||||||
|
if let Some(value) = &attribute.value {
|
||||||
|
match value.as_str() {
|
||||||
|
"active" => return DTLSRole::Client,
|
||||||
|
"passive" => return DTLSRole::Server,
|
||||||
|
_ => return DTLSRole::Auto,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return DTLSRole::Auto;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DTLSRole::Auto
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DTLSRole {
|
||||||
|
pub(crate) fn to_connection_role(self) -> ConnectionRole {
|
||||||
|
match self {
|
||||||
|
DTLSRole::Client => ConnectionRole::Active,
|
||||||
|
DTLSRole::Server => ConnectionRole::Passive,
|
||||||
|
DTLSRole::Auto => ConnectionRole::Actpass,
|
||||||
|
_ => ConnectionRole::Unspecified,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use std::io::Cursor;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use crate::error::Result;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dtls_role_string() {
|
||||||
|
let tests = vec![
|
||||||
|
(DTLSRole::Unspecified, "Unspecified"),
|
||||||
|
(DTLSRole::Auto, "auto"),
|
||||||
|
(DTLSRole::Client, "client"),
|
||||||
|
(DTLSRole::Server, "server"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (role, expected_string) in tests {
|
||||||
|
assert_eq!(role.to_string(), expected_string)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dtls_role_from_remote_sdp() -> Result<()> {
|
||||||
|
const NO_MEDIA: &str = "v=0
|
||||||
|
o=- 4596489990601351948 2 IN IP4 127.0.0.1
|
||||||
|
s=-
|
||||||
|
t=0 0
|
||||||
|
";
|
||||||
|
|
||||||
|
const MEDIA_NO_SETUP: &str = "v=0
|
||||||
|
o=- 4596489990601351948 2 IN IP4 127.0.0.1
|
||||||
|
s=-
|
||||||
|
t=0 0
|
||||||
|
m=application 47299 DTLS/SCTP 5000
|
||||||
|
c=IN IP4 192.168.20.129
|
||||||
|
";
|
||||||
|
|
||||||
|
const MEDIA_SETUP_DECLARED: &str = "v=0
|
||||||
|
o=- 4596489990601351948 2 IN IP4 127.0.0.1
|
||||||
|
s=-
|
||||||
|
t=0 0
|
||||||
|
m=application 47299 DTLS/SCTP 5000
|
||||||
|
c=IN IP4 192.168.20.129
|
||||||
|
a=setup:";
|
||||||
|
|
||||||
|
let tests = vec![
|
||||||
|
("No MediaDescriptions", NO_MEDIA.to_owned(), DTLSRole::Auto),
|
||||||
|
(
|
||||||
|
"MediaDescription, no setup",
|
||||||
|
MEDIA_NO_SETUP.to_owned(),
|
||||||
|
DTLSRole::Auto,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"MediaDescription, setup:actpass",
|
||||||
|
format!("{}{}\n", MEDIA_SETUP_DECLARED, "actpass"),
|
||||||
|
DTLSRole::Auto,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"MediaDescription, setup:passive",
|
||||||
|
format!("{}{}\n", MEDIA_SETUP_DECLARED, "passive"),
|
||||||
|
DTLSRole::Server,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"MediaDescription, setup:active",
|
||||||
|
format!("{}{}\n", MEDIA_SETUP_DECLARED, "active"),
|
||||||
|
DTLSRole::Client,
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (name, session_description_str, expected_role) in tests {
|
||||||
|
let mut reader = Cursor::new(session_description_str.as_bytes());
|
||||||
|
let session_description = SessionDescription::unmarshal(&mut reader)?;
|
||||||
|
assert_eq!(
|
||||||
|
DTLSRole::from(&session_description),
|
||||||
|
expected_role,
|
||||||
|
"{name} failed"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,117 @@
|
|||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
/// DTLSTransportState indicates the DTLS transport establishment state.
|
||||||
|
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
|
||||||
|
pub enum RTCDtlsTransportState {
|
||||||
|
#[default]
|
||||||
|
Unspecified = 0,
|
||||||
|
|
||||||
|
/// DTLSTransportStateNew indicates that DTLS has not started negotiating
|
||||||
|
/// yet.
|
||||||
|
New = 1,
|
||||||
|
|
||||||
|
/// DTLSTransportStateConnecting indicates that DTLS is in the process of
|
||||||
|
/// negotiating a secure connection and verifying the remote fingerprint.
|
||||||
|
Connecting = 2,
|
||||||
|
|
||||||
|
/// DTLSTransportStateConnected indicates that DTLS has completed
|
||||||
|
/// negotiation of a secure connection and verified the remote fingerprint.
|
||||||
|
Connected = 3,
|
||||||
|
|
||||||
|
/// DTLSTransportStateClosed indicates that the transport has been closed
|
||||||
|
/// intentionally as the result of receipt of a close_notify alert, or
|
||||||
|
/// calling close().
|
||||||
|
Closed = 4,
|
||||||
|
|
||||||
|
/// DTLSTransportStateFailed indicates that the transport has failed as
|
||||||
|
/// the result of an error (such as receipt of an error alert or failure to
|
||||||
|
/// validate the remote fingerprint).
|
||||||
|
Failed = 5,
|
||||||
|
}
|
||||||
|
|
||||||
|
const DTLS_TRANSPORT_STATE_NEW_STR: &str = "new";
|
||||||
|
const DTLS_TRANSPORT_STATE_CONNECTING_STR: &str = "connecting";
|
||||||
|
const DTLS_TRANSPORT_STATE_CONNECTED_STR: &str = "connected";
|
||||||
|
const DTLS_TRANSPORT_STATE_CLOSED_STR: &str = "closed";
|
||||||
|
const DTLS_TRANSPORT_STATE_FAILED_STR: &str = "failed";
|
||||||
|
|
||||||
|
impl From<&str> for RTCDtlsTransportState {
|
||||||
|
fn from(raw: &str) -> Self {
|
||||||
|
match raw {
|
||||||
|
DTLS_TRANSPORT_STATE_NEW_STR => RTCDtlsTransportState::New,
|
||||||
|
DTLS_TRANSPORT_STATE_CONNECTING_STR => RTCDtlsTransportState::Connecting,
|
||||||
|
DTLS_TRANSPORT_STATE_CONNECTED_STR => RTCDtlsTransportState::Connected,
|
||||||
|
DTLS_TRANSPORT_STATE_CLOSED_STR => RTCDtlsTransportState::Closed,
|
||||||
|
DTLS_TRANSPORT_STATE_FAILED_STR => RTCDtlsTransportState::Failed,
|
||||||
|
_ => RTCDtlsTransportState::Unspecified,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<u8> for RTCDtlsTransportState {
|
||||||
|
fn from(v: u8) -> Self {
|
||||||
|
match v {
|
||||||
|
1 => RTCDtlsTransportState::New,
|
||||||
|
2 => RTCDtlsTransportState::Connecting,
|
||||||
|
3 => RTCDtlsTransportState::Connected,
|
||||||
|
4 => RTCDtlsTransportState::Closed,
|
||||||
|
5 => RTCDtlsTransportState::Failed,
|
||||||
|
_ => RTCDtlsTransportState::Unspecified,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for RTCDtlsTransportState {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
let s = match *self {
|
||||||
|
RTCDtlsTransportState::New => DTLS_TRANSPORT_STATE_NEW_STR,
|
||||||
|
RTCDtlsTransportState::Connecting => DTLS_TRANSPORT_STATE_CONNECTING_STR,
|
||||||
|
RTCDtlsTransportState::Connected => DTLS_TRANSPORT_STATE_CONNECTED_STR,
|
||||||
|
RTCDtlsTransportState::Closed => DTLS_TRANSPORT_STATE_CLOSED_STR,
|
||||||
|
RTCDtlsTransportState::Failed => DTLS_TRANSPORT_STATE_FAILED_STR,
|
||||||
|
RTCDtlsTransportState::Unspecified => crate::UNSPECIFIED_STR,
|
||||||
|
};
|
||||||
|
write!(f, "{s}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_new_dtls_transport_state() {
|
||||||
|
let tests = vec![
|
||||||
|
(crate::UNSPECIFIED_STR, RTCDtlsTransportState::Unspecified),
|
||||||
|
("new", RTCDtlsTransportState::New),
|
||||||
|
("connecting", RTCDtlsTransportState::Connecting),
|
||||||
|
("connected", RTCDtlsTransportState::Connected),
|
||||||
|
("closed", RTCDtlsTransportState::Closed),
|
||||||
|
("failed", RTCDtlsTransportState::Failed),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (state_string, expected_state) in tests {
|
||||||
|
assert_eq!(
|
||||||
|
RTCDtlsTransportState::from(state_string),
|
||||||
|
expected_state,
|
||||||
|
"testCase: {expected_state}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dtls_transport_state_string() {
|
||||||
|
let tests = vec![
|
||||||
|
(RTCDtlsTransportState::Unspecified, crate::UNSPECIFIED_STR),
|
||||||
|
(RTCDtlsTransportState::New, "new"),
|
||||||
|
(RTCDtlsTransportState::Connecting, "connecting"),
|
||||||
|
(RTCDtlsTransportState::Connected, "connected"),
|
||||||
|
(RTCDtlsTransportState::Closed, "closed"),
|
||||||
|
(RTCDtlsTransportState::Failed, "failed"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (state, expected_string) in tests {
|
||||||
|
assert_eq!(state.to_string(), expected_string)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,204 @@
|
|||||||
|
use ice::mdns::MulticastDnsMode;
|
||||||
|
use ice::network_type::NetworkType;
|
||||||
|
use regex::Regex;
|
||||||
|
use tokio::time::Duration;
|
||||||
|
use waitgroup::WaitGroup;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use crate::api::media_engine::MediaEngine;
|
||||||
|
use crate::api::APIBuilder;
|
||||||
|
use crate::data_channel::RTCDataChannel;
|
||||||
|
use crate::ice_transport::ice_candidate::RTCIceCandidate;
|
||||||
|
use crate::peer_connection::configuration::RTCConfiguration;
|
||||||
|
use crate::peer_connection::peer_connection_state::RTCPeerConnectionState;
|
||||||
|
use crate::peer_connection::peer_connection_test::{
|
||||||
|
close_pair_now, new_pair, signal_pair, until_connection_state,
|
||||||
|
};
|
||||||
|
|
||||||
|
//use log::LevelFilter;
|
||||||
|
//use std::io::Write;
|
||||||
|
|
||||||
|
// An invalid fingerprint MUST cause PeerConnectionState to go to PeerConnectionStateFailed
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_invalid_fingerprint_causes_failed() -> Result<()> {
|
||||||
|
/*env_logger::Builder::new()
|
||||||
|
.format(|buf, record| {
|
||||||
|
writeln!(
|
||||||
|
buf,
|
||||||
|
"{}:{} [{}] {} - {}",
|
||||||
|
record.file().unwrap_or("unknown"),
|
||||||
|
record.line().unwrap_or(0),
|
||||||
|
record.level(),
|
||||||
|
chrono::Local::now().format("%H:%M:%S.%6f"),
|
||||||
|
record.args()
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.filter(None, LevelFilter::Trace)
|
||||||
|
.init();*/
|
||||||
|
|
||||||
|
let mut m = MediaEngine::default();
|
||||||
|
m.register_default_codecs()?;
|
||||||
|
let api = APIBuilder::new().with_media_engine(m).build();
|
||||||
|
|
||||||
|
let (mut pc_offer, mut pc_answer) = new_pair(&api).await?;
|
||||||
|
|
||||||
|
pc_answer.on_data_channel(Box::new(|_: Arc<RTCDataChannel>| {
|
||||||
|
panic!("A DataChannel must not be created when Fingerprint verification fails");
|
||||||
|
}));
|
||||||
|
|
||||||
|
let (offer_chan_tx, mut offer_chan_rx) = mpsc::channel::<()>(1);
|
||||||
|
|
||||||
|
let offer_chan_tx = Arc::new(offer_chan_tx);
|
||||||
|
pc_offer.on_ice_candidate(Box::new(move |candidate: Option<RTCIceCandidate>| {
|
||||||
|
let offer_chan_tx2 = Arc::clone(&offer_chan_tx);
|
||||||
|
Box::pin(async move {
|
||||||
|
if candidate.is_none() {
|
||||||
|
let _ = offer_chan_tx2.send(()).await;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}));
|
||||||
|
|
||||||
|
let offer_connection_has_failed = WaitGroup::new();
|
||||||
|
until_connection_state(
|
||||||
|
&mut pc_offer,
|
||||||
|
&offer_connection_has_failed,
|
||||||
|
RTCPeerConnectionState::Failed,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
let answer_connection_has_failed = WaitGroup::new();
|
||||||
|
until_connection_state(
|
||||||
|
&mut pc_answer,
|
||||||
|
&answer_connection_has_failed,
|
||||||
|
RTCPeerConnectionState::Failed,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let _ = pc_offer
|
||||||
|
.create_data_channel("unusedDataChannel", None)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let offer = pc_offer.create_offer(None).await?;
|
||||||
|
pc_offer.set_local_description(offer).await?;
|
||||||
|
|
||||||
|
let timeout = tokio::time::sleep(Duration::from_secs(1));
|
||||||
|
tokio::pin!(timeout);
|
||||||
|
|
||||||
|
tokio::select! {
|
||||||
|
_ = offer_chan_rx.recv() =>{
|
||||||
|
let mut offer = pc_offer.pending_local_description().await.unwrap();
|
||||||
|
|
||||||
|
log::trace!("receiving pending local desc: {:?}", offer);
|
||||||
|
|
||||||
|
// Replace with invalid fingerprint
|
||||||
|
let re = Regex::new(r"sha-256 (.*?)\r").unwrap();
|
||||||
|
offer.sdp = re.replace_all(offer.sdp.as_str(), "sha-256 AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA\r").to_string();
|
||||||
|
|
||||||
|
pc_answer.set_remote_description(offer).await?;
|
||||||
|
|
||||||
|
let mut answer = pc_answer.create_answer(None).await?;
|
||||||
|
|
||||||
|
pc_answer.set_local_description(answer.clone()).await?;
|
||||||
|
|
||||||
|
answer.sdp = re.replace_all(answer.sdp.as_str(), "sha-256 AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA\r").to_string();
|
||||||
|
|
||||||
|
pc_offer.set_remote_description(answer).await?;
|
||||||
|
}
|
||||||
|
_ = timeout.as_mut() =>{
|
||||||
|
panic!("timed out waiting to receive offer");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log::trace!("offer_connection_has_failed wait begin");
|
||||||
|
|
||||||
|
offer_connection_has_failed.wait().await;
|
||||||
|
answer_connection_has_failed.wait().await;
|
||||||
|
|
||||||
|
log::trace!("offer_connection_has_failed wait end");
|
||||||
|
{
|
||||||
|
let transport = pc_offer.sctp().transport();
|
||||||
|
assert_eq!(transport.state(), RTCDtlsTransportState::Failed);
|
||||||
|
assert!(transport.conn().await.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let transport = pc_answer.sctp().transport();
|
||||||
|
assert_eq!(transport.state(), RTCDtlsTransportState::Failed);
|
||||||
|
assert!(transport.conn().await.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
close_pair_now(&pc_offer, &pc_answer).await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run_test(r: DTLSRole) -> Result<()> {
|
||||||
|
let mut offer_s = SettingEngine::default();
|
||||||
|
offer_s.set_answering_dtls_role(r)?;
|
||||||
|
offer_s.set_ice_multicast_dns_mode(MulticastDnsMode::Disabled);
|
||||||
|
offer_s.set_network_types(vec![NetworkType::Udp4]);
|
||||||
|
let mut offer_pc = APIBuilder::new()
|
||||||
|
.with_setting_engine(offer_s)
|
||||||
|
.build()
|
||||||
|
.new_peer_connection(RTCConfiguration::default())
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let mut answer_s = SettingEngine::default();
|
||||||
|
answer_s.set_answering_dtls_role(r)?;
|
||||||
|
answer_s.set_ice_multicast_dns_mode(MulticastDnsMode::Disabled);
|
||||||
|
answer_s.set_network_types(vec![NetworkType::Udp4]);
|
||||||
|
let mut answer_pc = APIBuilder::new()
|
||||||
|
.with_setting_engine(answer_s)
|
||||||
|
.build()
|
||||||
|
.new_peer_connection(RTCConfiguration::default())
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
signal_pair(&mut offer_pc, &mut answer_pc).await?;
|
||||||
|
|
||||||
|
let wg = WaitGroup::new();
|
||||||
|
until_connection_state(&mut answer_pc, &wg, RTCPeerConnectionState::Connected).await;
|
||||||
|
wg.wait().await;
|
||||||
|
|
||||||
|
close_pair_now(&offer_pc, &answer_pc).await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_peer_connection_dtls_role_setting_engine_server() -> Result<()> {
|
||||||
|
/*env_logger::Builder::new()
|
||||||
|
.format(|buf, record| {
|
||||||
|
writeln!(
|
||||||
|
buf,
|
||||||
|
"{}:{} [{}] {} - {}",
|
||||||
|
record.file().unwrap_or("unknown"),
|
||||||
|
record.line().unwrap_or(0),
|
||||||
|
record.level(),
|
||||||
|
chrono::Local::now().format("%H:%M:%S.%6f"),
|
||||||
|
record.args()
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.filter(None, LevelFilter::Trace)
|
||||||
|
.init();*/
|
||||||
|
|
||||||
|
run_test(DTLSRole::Server).await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_peer_connection_dtls_role_setting_engine_client() -> Result<()> {
|
||||||
|
/*env_logger::Builder::new()
|
||||||
|
.format(|buf, record| {
|
||||||
|
writeln!(
|
||||||
|
buf,
|
||||||
|
"{}:{} [{}] {} - {}",
|
||||||
|
record.file().unwrap_or("unknown"),
|
||||||
|
record.line().unwrap_or(0),
|
||||||
|
record.level(),
|
||||||
|
chrono::Local::now().format("%H:%M:%S.%6f"),
|
||||||
|
record.args()
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.filter(None, LevelFilter::Trace)
|
||||||
|
.init();*/
|
||||||
|
|
||||||
|
run_test(DTLSRole::Client).await
|
||||||
|
}
|
||||||
+616
@@ -0,0 +1,616 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use arc_swap::ArcSwapOption;
|
||||||
|
use bytes::Bytes;
|
||||||
|
use dtls::config::ClientAuthType;
|
||||||
|
use dtls::conn::DTLSConn;
|
||||||
|
use dtls::extension::extension_use_srtp::SrtpProtectionProfile;
|
||||||
|
use dtls_role::*;
|
||||||
|
use interceptor::stream_info::StreamInfo;
|
||||||
|
use interceptor::{Interceptor, RTCPReader, RTPReader};
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
use srtp::protection_profile::ProtectionProfile;
|
||||||
|
use srtp::session::Session;
|
||||||
|
use srtp::stream::Stream;
|
||||||
|
use tokio::sync::{mpsc, Mutex};
|
||||||
|
use util::Conn;
|
||||||
|
|
||||||
|
use crate::api::setting_engine::SettingEngine;
|
||||||
|
use crate::dtls_transport::dtls_parameters::DTLSParameters;
|
||||||
|
use crate::dtls_transport::dtls_transport_state::RTCDtlsTransportState;
|
||||||
|
use crate::error::{flatten_errs, Error, Result};
|
||||||
|
use crate::ice_transport::ice_role::RTCIceRole;
|
||||||
|
use crate::ice_transport::ice_transport_state::RTCIceTransportState;
|
||||||
|
use crate::ice_transport::RTCIceTransport;
|
||||||
|
use crate::mux::endpoint::Endpoint;
|
||||||
|
use crate::mux::mux_func::{match_dtls, match_srtcp, match_srtp, MatchFunc};
|
||||||
|
use crate::peer_connection::certificate::RTCCertificate;
|
||||||
|
use crate::rtp_transceiver::SSRC;
|
||||||
|
use crate::stats::stats_collector::StatsCollector;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod dtls_transport_test;
|
||||||
|
|
||||||
|
pub mod dtls_fingerprint;
|
||||||
|
pub mod dtls_parameters;
|
||||||
|
pub mod dtls_role;
|
||||||
|
pub mod dtls_transport_state;
|
||||||
|
|
||||||
|
pub(crate) fn default_srtp_protection_profiles() -> Vec<SrtpProtectionProfile> {
|
||||||
|
vec![
|
||||||
|
SrtpProtectionProfile::Srtp_Aead_Aes_128_Gcm,
|
||||||
|
SrtpProtectionProfile::Srtp_Aes128_Cm_Hmac_Sha1_80,
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type OnDTLSTransportStateChangeHdlrFn = Box<
|
||||||
|
dyn (FnMut(RTCDtlsTransportState) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>)
|
||||||
|
+ Send
|
||||||
|
+ Sync,
|
||||||
|
>;
|
||||||
|
|
||||||
|
/// DTLSTransport allows an application access to information about the DTLS
|
||||||
|
/// transport over which RTP and RTCP packets are sent and received by
|
||||||
|
/// RTPSender and RTPReceiver, as well other data such as SCTP packets sent
|
||||||
|
/// and received by data channels.
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct RTCDtlsTransport {
|
||||||
|
pub(crate) ice_transport: Arc<RTCIceTransport>,
|
||||||
|
pub(crate) certificates: Vec<RTCCertificate>,
|
||||||
|
pub(crate) setting_engine: Arc<SettingEngine>,
|
||||||
|
|
||||||
|
pub(crate) remote_parameters: Mutex<DTLSParameters>,
|
||||||
|
pub(crate) remote_certificate: Mutex<Bytes>,
|
||||||
|
pub(crate) state: AtomicU8, //DTLSTransportState,
|
||||||
|
pub(crate) srtp_protection_profile: Mutex<ProtectionProfile>,
|
||||||
|
pub(crate) on_state_change_handler: ArcSwapOption<Mutex<OnDTLSTransportStateChangeHdlrFn>>,
|
||||||
|
pub(crate) conn: Mutex<Option<Arc<DTLSConn>>>,
|
||||||
|
|
||||||
|
pub(crate) srtp_session: Mutex<Option<Arc<Session>>>,
|
||||||
|
pub(crate) srtcp_session: Mutex<Option<Arc<Session>>>,
|
||||||
|
pub(crate) srtp_endpoint: Mutex<Option<Arc<Endpoint>>>,
|
||||||
|
pub(crate) srtcp_endpoint: Mutex<Option<Arc<Endpoint>>>,
|
||||||
|
|
||||||
|
pub(crate) simulcast_streams: Mutex<HashMap<SSRC, Arc<Stream>>>,
|
||||||
|
|
||||||
|
pub(crate) srtp_ready_signal: Arc<AtomicBool>,
|
||||||
|
pub(crate) srtp_ready_tx: Mutex<Option<mpsc::Sender<()>>>,
|
||||||
|
pub(crate) srtp_ready_rx: Mutex<Option<mpsc::Receiver<()>>>,
|
||||||
|
|
||||||
|
pub(crate) dtls_matcher: Option<MatchFunc>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RTCDtlsTransport {
|
||||||
|
pub(crate) fn new(
|
||||||
|
ice_transport: Arc<RTCIceTransport>,
|
||||||
|
certificates: Vec<RTCCertificate>,
|
||||||
|
setting_engine: Arc<SettingEngine>,
|
||||||
|
) -> Self {
|
||||||
|
let (srtp_ready_tx, srtp_ready_rx) = mpsc::channel(1);
|
||||||
|
RTCDtlsTransport {
|
||||||
|
ice_transport,
|
||||||
|
certificates,
|
||||||
|
setting_engine,
|
||||||
|
srtp_ready_signal: Arc::new(AtomicBool::new(false)),
|
||||||
|
srtp_ready_tx: Mutex::new(Some(srtp_ready_tx)),
|
||||||
|
srtp_ready_rx: Mutex::new(Some(srtp_ready_rx)),
|
||||||
|
state: AtomicU8::new(RTCDtlsTransportState::New as u8),
|
||||||
|
dtls_matcher: Some(Box::new(match_dtls)),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn conn(&self) -> Option<Arc<DTLSConn>> {
|
||||||
|
let conn = self.conn.lock().await;
|
||||||
|
conn.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// returns the currently-configured ICETransport or None
|
||||||
|
/// if one has not been configured
|
||||||
|
pub fn ice_transport(&self) -> &RTCIceTransport {
|
||||||
|
&self.ice_transport
|
||||||
|
}
|
||||||
|
|
||||||
|
/// state_change requires the caller holds the lock
|
||||||
|
async fn state_change(&self, state: RTCDtlsTransportState) {
|
||||||
|
self.state.store(state as u8, Ordering::SeqCst);
|
||||||
|
if let Some(handler) = &*self.on_state_change_handler.load() {
|
||||||
|
let mut f = handler.lock().await;
|
||||||
|
f(state).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// on_state_change sets a handler that is fired when the DTLS
|
||||||
|
/// connection state changes.
|
||||||
|
pub fn on_state_change(&self, f: OnDTLSTransportStateChangeHdlrFn) {
|
||||||
|
self.on_state_change_handler
|
||||||
|
.store(Some(Arc::new(Mutex::new(f))));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// state returns the current dtls_transport transport state.
|
||||||
|
pub fn state(&self) -> RTCDtlsTransportState {
|
||||||
|
self.state.load(Ordering::SeqCst).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// write_rtcp sends a user provided RTCP packet to the connected peer. If no peer is connected the
|
||||||
|
/// packet is discarded.
|
||||||
|
pub async fn write_rtcp(
|
||||||
|
&self,
|
||||||
|
pkts: &[Box<dyn rtcp::packet::Packet + Send + Sync>],
|
||||||
|
) -> Result<usize> {
|
||||||
|
let srtcp_session = self.srtcp_session.lock().await;
|
||||||
|
if let Some(srtcp_session) = &*srtcp_session {
|
||||||
|
let raw = rtcp::packet::marshal(pkts)?;
|
||||||
|
Ok(srtcp_session.write(&raw, false).await?)
|
||||||
|
} else {
|
||||||
|
Ok(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// get_local_parameters returns the DTLS parameters of the local DTLSTransport upon construction.
|
||||||
|
pub fn get_local_parameters(&self) -> Result<DTLSParameters> {
|
||||||
|
let mut fingerprints = vec![];
|
||||||
|
|
||||||
|
for c in &self.certificates {
|
||||||
|
fingerprints.extend(c.get_fingerprints());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(DTLSParameters {
|
||||||
|
role: DTLSRole::Auto, // always returns the default role
|
||||||
|
fingerprints,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// get_remote_certificate returns the certificate chain in use by the remote side
|
||||||
|
/// returns an empty list prior to selection of the remote certificate
|
||||||
|
pub async fn get_remote_certificate(&self) -> Bytes {
|
||||||
|
let remote_certificate = self.remote_certificate.lock().await;
|
||||||
|
remote_certificate.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn start_srtp(&self) -> Result<()> {
|
||||||
|
let profile = {
|
||||||
|
let srtp_protection_profile = self.srtp_protection_profile.lock().await;
|
||||||
|
*srtp_protection_profile
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut srtp_config = srtp::config::Config {
|
||||||
|
profile,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
if self.setting_engine.replay_protection.srtp != 0 {
|
||||||
|
srtp_config.remote_rtp_options = Some(srtp::option::srtp_replay_protection(
|
||||||
|
self.setting_engine.replay_protection.srtp,
|
||||||
|
));
|
||||||
|
} else if self.setting_engine.disable_srtp_replay_protection {
|
||||||
|
srtp_config.remote_rtp_options = Some(srtp::option::srtp_no_replay_protection());
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(conn) = self.conn().await {
|
||||||
|
let conn_state = conn.connection_state().await;
|
||||||
|
srtp_config
|
||||||
|
.extract_session_keys_from_dtls(conn_state, self.role().await == DTLSRole::Client)
|
||||||
|
.await?;
|
||||||
|
} else {
|
||||||
|
return Err(Error::ErrDtlsTransportNotStarted);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut srtp_session = self.srtp_session.lock().await;
|
||||||
|
*srtp_session = {
|
||||||
|
let se = self.srtp_endpoint.lock().await;
|
||||||
|
if let Some(srtp_endpoint) = &*se {
|
||||||
|
Some(Arc::new(
|
||||||
|
Session::new(
|
||||||
|
Arc::clone(srtp_endpoint) as Arc<dyn Conn + Send + Sync>,
|
||||||
|
srtp_config,
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
.await?,
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut srtcp_config = srtp::config::Config {
|
||||||
|
profile,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
if self.setting_engine.replay_protection.srtcp != 0 {
|
||||||
|
srtcp_config.remote_rtcp_options = Some(srtp::option::srtcp_replay_protection(
|
||||||
|
self.setting_engine.replay_protection.srtcp,
|
||||||
|
));
|
||||||
|
} else if self.setting_engine.disable_srtcp_replay_protection {
|
||||||
|
srtcp_config.remote_rtcp_options = Some(srtp::option::srtcp_no_replay_protection());
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(conn) = self.conn().await {
|
||||||
|
let conn_state = conn.connection_state().await;
|
||||||
|
srtcp_config
|
||||||
|
.extract_session_keys_from_dtls(conn_state, self.role().await == DTLSRole::Client)
|
||||||
|
.await?;
|
||||||
|
} else {
|
||||||
|
return Err(Error::ErrDtlsTransportNotStarted);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut srtcp_session = self.srtcp_session.lock().await;
|
||||||
|
*srtcp_session = {
|
||||||
|
let se = self.srtcp_endpoint.lock().await;
|
||||||
|
if let Some(srtcp_endpoint) = &*se {
|
||||||
|
Some(Arc::new(
|
||||||
|
Session::new(
|
||||||
|
Arc::clone(srtcp_endpoint) as Arc<dyn Conn + Send + Sync>,
|
||||||
|
srtcp_config,
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
.await?,
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut srtp_ready_tx = self.srtp_ready_tx.lock().await;
|
||||||
|
srtp_ready_tx.take();
|
||||||
|
if srtp_ready_tx.is_none() {
|
||||||
|
self.srtp_ready_signal.store(true, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn get_srtp_session(&self) -> Option<Arc<Session>> {
|
||||||
|
let srtp_session = self.srtp_session.lock().await;
|
||||||
|
srtp_session.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn get_srtcp_session(&self) -> Option<Arc<Session>> {
|
||||||
|
let srtcp_session = self.srtcp_session.lock().await;
|
||||||
|
srtcp_session.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn role(&self) -> DTLSRole {
|
||||||
|
// If remote has an explicit role use the inverse
|
||||||
|
{
|
||||||
|
let remote_parameters = self.remote_parameters.lock().await;
|
||||||
|
match remote_parameters.role {
|
||||||
|
DTLSRole::Client => return DTLSRole::Server,
|
||||||
|
DTLSRole::Server => return DTLSRole::Client,
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// If SettingEngine has an explicit role
|
||||||
|
match self.setting_engine.answering_dtls_role {
|
||||||
|
DTLSRole::Server => return DTLSRole::Server,
|
||||||
|
DTLSRole::Client => return DTLSRole::Client,
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Remote was auto and no explicit role was configured via SettingEngine
|
||||||
|
if self.ice_transport.role().await == RTCIceRole::Controlling {
|
||||||
|
return DTLSRole::Server;
|
||||||
|
}
|
||||||
|
|
||||||
|
DEFAULT_DTLS_ROLE_ANSWER
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn collect_stats(&self, collector: &StatsCollector) {
|
||||||
|
for cert in &self.certificates {
|
||||||
|
cert.collect_stats(collector).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn prepare_transport(
|
||||||
|
&self,
|
||||||
|
remote_parameters: DTLSParameters,
|
||||||
|
) -> Result<(DTLSRole, dtls::config::Config)> {
|
||||||
|
self.ensure_ice_conn()?;
|
||||||
|
|
||||||
|
if self.state() != RTCDtlsTransportState::New {
|
||||||
|
return Err(Error::ErrInvalidDTLSStart);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut srtp_endpoint = self.srtp_endpoint.lock().await;
|
||||||
|
*srtp_endpoint = self.ice_transport.new_endpoint(Box::new(match_srtp)).await;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let mut srtcp_endpoint = self.srtcp_endpoint.lock().await;
|
||||||
|
*srtcp_endpoint = self.ice_transport.new_endpoint(Box::new(match_srtcp)).await;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let mut rp = self.remote_parameters.lock().await;
|
||||||
|
*rp = remote_parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
let certificate = if let Some(cert) = self.certificates.first() {
|
||||||
|
cert.dtls_certificate.clone()
|
||||||
|
} else {
|
||||||
|
return Err(Error::ErrNonCertificate);
|
||||||
|
};
|
||||||
|
self.state_change(RTCDtlsTransportState::Connecting).await;
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
self.role().await,
|
||||||
|
dtls::config::Config {
|
||||||
|
certificates: vec![certificate],
|
||||||
|
srtp_protection_profiles: if !self
|
||||||
|
.setting_engine
|
||||||
|
.srtp_protection_profiles
|
||||||
|
.is_empty()
|
||||||
|
{
|
||||||
|
self.setting_engine.srtp_protection_profiles.clone()
|
||||||
|
} else {
|
||||||
|
default_srtp_protection_profiles()
|
||||||
|
},
|
||||||
|
client_auth: ClientAuthType::RequireAnyClientCert,
|
||||||
|
insecure_skip_verify: true,
|
||||||
|
insecure_verification: self.setting_engine.allow_insecure_verification_algorithm,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// start DTLS transport negotiation with the parameters of the remote DTLS transport
|
||||||
|
pub async fn start(&self, remote_parameters: DTLSParameters) -> Result<()> {
|
||||||
|
let dtls_conn_result = if let Some(dtls_endpoint) =
|
||||||
|
self.ice_transport.new_endpoint(Box::new(match_dtls)).await
|
||||||
|
{
|
||||||
|
let (role, mut dtls_config) = self.prepare_transport(remote_parameters).await?;
|
||||||
|
if self.setting_engine.replay_protection.dtls != 0 {
|
||||||
|
dtls_config.replay_protection_window = self.setting_engine.replay_protection.dtls;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Connect as DTLS Client/Server, function is blocking and we
|
||||||
|
// must not hold the DTLSTransport lock
|
||||||
|
if role == DTLSRole::Client {
|
||||||
|
dtls::conn::DTLSConn::new(
|
||||||
|
dtls_endpoint as Arc<dyn Conn + Send + Sync>,
|
||||||
|
dtls_config,
|
||||||
|
true,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
} else {
|
||||||
|
dtls::conn::DTLSConn::new(
|
||||||
|
dtls_endpoint as Arc<dyn Conn + Send + Sync>,
|
||||||
|
dtls_config,
|
||||||
|
false,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(dtls::Error::Other(
|
||||||
|
"ice_transport.new_endpoint failed".to_owned(),
|
||||||
|
))
|
||||||
|
};
|
||||||
|
|
||||||
|
let dtls_conn = match dtls_conn_result {
|
||||||
|
Ok(dtls_conn) => dtls_conn,
|
||||||
|
Err(err) => {
|
||||||
|
self.state_change(RTCDtlsTransportState::Failed).await;
|
||||||
|
return Err(err.into());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let srtp_profile = dtls_conn.selected_srtpprotection_profile();
|
||||||
|
{
|
||||||
|
let mut srtp_protection_profile = self.srtp_protection_profile.lock().await;
|
||||||
|
*srtp_protection_profile = match srtp_profile {
|
||||||
|
dtls::extension::extension_use_srtp::SrtpProtectionProfile::Srtp_Aead_Aes_128_Gcm => {
|
||||||
|
srtp::protection_profile::ProtectionProfile::AeadAes128Gcm
|
||||||
|
}
|
||||||
|
dtls::extension::extension_use_srtp::SrtpProtectionProfile::Srtp_Aes128_Cm_Hmac_Sha1_80 => {
|
||||||
|
srtp::protection_profile::ProtectionProfile::Aes128CmHmacSha1_80
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
if let Err(err) = dtls_conn.close().await {
|
||||||
|
log::error!("{}", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.state_change(RTCDtlsTransportState::Failed).await;
|
||||||
|
return Err(Error::ErrNoSRTPProtectionProfile);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the fingerprint if a certificate was exchanged
|
||||||
|
let remote_certs = &dtls_conn.connection_state().await.peer_certificates;
|
||||||
|
if remote_certs.is_empty() {
|
||||||
|
if let Err(err) = dtls_conn.close().await {
|
||||||
|
log::error!("{}", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.state_change(RTCDtlsTransportState::Failed).await;
|
||||||
|
return Err(Error::ErrNoRemoteCertificate);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut remote_certificate = self.remote_certificate.lock().await;
|
||||||
|
*remote_certificate = Bytes::from(remote_certs[0].clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
if !self
|
||||||
|
.setting_engine
|
||||||
|
.disable_certificate_fingerprint_verification
|
||||||
|
{
|
||||||
|
if let Err(err) = self.validate_fingerprint(&remote_certs[0]).await {
|
||||||
|
if let Err(close_err) = dtls_conn.close().await {
|
||||||
|
log::error!("{}", close_err);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.state_change(RTCDtlsTransportState::Failed).await;
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut conn = self.conn.lock().await;
|
||||||
|
*conn = Some(Arc::new(dtls_conn));
|
||||||
|
}
|
||||||
|
self.state_change(RTCDtlsTransportState::Connected).await;
|
||||||
|
|
||||||
|
self.start_srtp().await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// stops and closes the DTLSTransport object.
|
||||||
|
pub async fn stop(&self) -> Result<()> {
|
||||||
|
// Try closing everything and collect the errors
|
||||||
|
let mut close_errs: Vec<Error> = vec![];
|
||||||
|
{
|
||||||
|
let srtp_session = {
|
||||||
|
let mut srtp_session = self.srtp_session.lock().await;
|
||||||
|
srtp_session.take()
|
||||||
|
};
|
||||||
|
if let Some(srtp_session) = srtp_session {
|
||||||
|
match srtp_session.close().await {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(err) => {
|
||||||
|
close_errs.push(err.into());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let srtcp_session = {
|
||||||
|
let mut srtcp_session = self.srtcp_session.lock().await;
|
||||||
|
srtcp_session.take()
|
||||||
|
};
|
||||||
|
if let Some(srtcp_session) = srtcp_session {
|
||||||
|
match srtcp_session.close().await {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(err) => {
|
||||||
|
close_errs.push(err.into());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let simulcast_streams: Vec<Arc<Stream>> = {
|
||||||
|
let mut simulcast_streams = self.simulcast_streams.lock().await;
|
||||||
|
simulcast_streams.drain().map(|(_, v)| v).collect()
|
||||||
|
};
|
||||||
|
for ss in simulcast_streams {
|
||||||
|
match ss.close().await {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(err) => {
|
||||||
|
close_errs.push(Error::new(format!(
|
||||||
|
"simulcast_streams ssrc={}: {}",
|
||||||
|
ss.get_ssrc(),
|
||||||
|
err
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(conn) = self.conn().await {
|
||||||
|
// dtls_transport connection may be closed on sctp close.
|
||||||
|
match conn.close().await {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(err) => {
|
||||||
|
if err.to_string() != dtls::Error::ErrConnClosed.to_string() {
|
||||||
|
close_errs.push(err.into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.state_change(RTCDtlsTransportState::Closed).await;
|
||||||
|
|
||||||
|
flatten_errs(close_errs)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn validate_fingerprint(&self, remote_cert: &[u8]) -> Result<()> {
|
||||||
|
let remote_parameters = self.remote_parameters.lock().await;
|
||||||
|
for fp in &remote_parameters.fingerprints {
|
||||||
|
if fp.algorithm != "sha-256" {
|
||||||
|
return Err(Error::ErrUnsupportedFingerprintAlgorithm);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut h = Sha256::new();
|
||||||
|
h.update(remote_cert);
|
||||||
|
let hashed = h.finalize();
|
||||||
|
let values: Vec<String> = hashed.iter().map(|x| format! {"{x:02x}"}).collect();
|
||||||
|
let remote_value = values.join(":").to_lowercase();
|
||||||
|
|
||||||
|
if remote_value == fp.value.to_lowercase() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(Error::ErrNoMatchingCertificateFingerprint)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn ensure_ice_conn(&self) -> Result<()> {
|
||||||
|
if self.ice_transport.state() == RTCIceTransportState::New {
|
||||||
|
Err(Error::ErrICEConnectionNotStarted)
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn store_simulcast_stream(&self, ssrc: SSRC, stream: Arc<Stream>) {
|
||||||
|
let mut simulcast_streams = self.simulcast_streams.lock().await;
|
||||||
|
simulcast_streams.insert(ssrc, stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn remove_simulcast_stream(&self, ssrc: SSRC) {
|
||||||
|
let mut simulcast_streams = self.simulcast_streams.lock().await;
|
||||||
|
simulcast_streams.remove(&ssrc);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn streams_for_ssrc(
|
||||||
|
&self,
|
||||||
|
ssrc: SSRC,
|
||||||
|
stream_info: &StreamInfo,
|
||||||
|
interceptor: &Arc<dyn Interceptor + Send + Sync>,
|
||||||
|
) -> Result<(
|
||||||
|
Arc<srtp::stream::Stream>,
|
||||||
|
Arc<dyn RTPReader + Send + Sync>,
|
||||||
|
Arc<srtp::stream::Stream>,
|
||||||
|
Arc<dyn RTCPReader + Send + Sync>,
|
||||||
|
)> {
|
||||||
|
let srtp_session = self
|
||||||
|
.get_srtp_session()
|
||||||
|
.await
|
||||||
|
.ok_or(Error::ErrDtlsTransportNotStarted)?;
|
||||||
|
//log::debug!("streams_for_ssrc: srtp_session.listen ssrc={}", ssrc);
|
||||||
|
let rtp_read_stream = srtp_session.open(ssrc).await;
|
||||||
|
let rtp_stream_reader = Arc::clone(&rtp_read_stream) as Arc<dyn RTPReader + Send + Sync>;
|
||||||
|
let rtp_interceptor = interceptor
|
||||||
|
.bind_remote_stream(stream_info, rtp_stream_reader)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let srtcp_session = self
|
||||||
|
.get_srtcp_session()
|
||||||
|
.await
|
||||||
|
.ok_or(Error::ErrDtlsTransportNotStarted)?;
|
||||||
|
//log::debug!("streams_for_ssrc: srtcp_session.listen ssrc={}", ssrc);
|
||||||
|
let rtcp_read_stream = srtcp_session.open(ssrc).await;
|
||||||
|
let rtcp_stream_reader = Arc::clone(&rtcp_read_stream) as Arc<dyn RTCPReader + Send + Sync>;
|
||||||
|
let rtcp_interceptor = interceptor.bind_rtcp_reader(rtcp_stream_reader).await;
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
rtp_read_stream,
|
||||||
|
rtp_interceptor,
|
||||||
|
rtcp_read_stream,
|
||||||
|
rtcp_interceptor,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
Vendored
+463
@@ -0,0 +1,463 @@
|
|||||||
|
use std::future::Future;
|
||||||
|
use std::num::ParseIntError;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::string::FromUtf8Error;
|
||||||
|
|
||||||
|
use rcgen::RcgenError;
|
||||||
|
use thiserror::Error;
|
||||||
|
use tokio::sync::mpsc::error::SendError as MpscSendError;
|
||||||
|
|
||||||
|
use crate::peer_connection::sdp::sdp_type::RTCSdpType;
|
||||||
|
use crate::peer_connection::signaling_state::RTCSignalingState;
|
||||||
|
use crate::rtp_transceiver::rtp_receiver;
|
||||||
|
|
||||||
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
|
#[derive(Error, Debug, PartialEq)]
|
||||||
|
#[non_exhaustive]
|
||||||
|
pub enum Error {
|
||||||
|
/// ErrUnknownType indicates an error with Unknown info.
|
||||||
|
#[error("unknown")]
|
||||||
|
ErrUnknownType,
|
||||||
|
|
||||||
|
/// ErrConnectionClosed indicates an operation executed after connection
|
||||||
|
/// has already been closed.
|
||||||
|
#[error("connection closed")]
|
||||||
|
ErrConnectionClosed,
|
||||||
|
|
||||||
|
/// ErrDataChannelNotOpen indicates an operation executed when the data
|
||||||
|
/// channel is not (yet) open.
|
||||||
|
#[error("data channel not open")]
|
||||||
|
ErrDataChannelNotOpen,
|
||||||
|
|
||||||
|
/// ErrCertificateExpired indicates that an x509 certificate has expired.
|
||||||
|
#[error("x509Cert expired")]
|
||||||
|
ErrCertificateExpired,
|
||||||
|
|
||||||
|
/// ErrNoTurnCredentials indicates that a TURN server URL was provided
|
||||||
|
/// without required credentials.
|
||||||
|
#[error("turn server credentials required")]
|
||||||
|
ErrNoTurnCredentials,
|
||||||
|
|
||||||
|
/// ErrTurnCredentials indicates that provided TURN credentials are partial
|
||||||
|
/// or malformed.
|
||||||
|
#[error("invalid turn server credentials")]
|
||||||
|
ErrTurnCredentials,
|
||||||
|
|
||||||
|
/// ErrExistingTrack indicates that a track already exists.
|
||||||
|
#[error("track already exists")]
|
||||||
|
ErrExistingTrack,
|
||||||
|
|
||||||
|
/// ErrPrivateKeyType indicates that a particular private key encryption
|
||||||
|
/// chosen to generate a certificate is not supported.
|
||||||
|
#[error("private key type not supported")]
|
||||||
|
ErrPrivateKeyType,
|
||||||
|
|
||||||
|
/// ErrModifyingPeerIdentity indicates that an attempt to modify
|
||||||
|
/// PeerIdentity was made after PeerConnection has been initialized.
|
||||||
|
#[error("peerIdentity cannot be modified")]
|
||||||
|
ErrModifyingPeerIdentity,
|
||||||
|
|
||||||
|
/// ErrModifyingCertificates indicates that an attempt to modify
|
||||||
|
/// Certificates was made after PeerConnection has been initialized.
|
||||||
|
#[error("certificates cannot be modified")]
|
||||||
|
ErrModifyingCertificates,
|
||||||
|
|
||||||
|
/// ErrNonCertificate indicates that there is no certificate
|
||||||
|
#[error("no certificate")]
|
||||||
|
ErrNonCertificate,
|
||||||
|
|
||||||
|
/// ErrModifyingBundlePolicy indicates that an attempt to modify
|
||||||
|
/// BundlePolicy was made after PeerConnection has been initialized.
|
||||||
|
#[error("bundle policy cannot be modified")]
|
||||||
|
ErrModifyingBundlePolicy,
|
||||||
|
|
||||||
|
/// ErrModifyingRTCPMuxPolicy indicates that an attempt to modify
|
||||||
|
/// RTCPMuxPolicy was made after PeerConnection has been initialized.
|
||||||
|
#[error("rtcp mux policy cannot be modified")]
|
||||||
|
ErrModifyingRTCPMuxPolicy,
|
||||||
|
|
||||||
|
/// ErrModifyingICECandidatePoolSize indicates that an attempt to modify
|
||||||
|
/// ICECandidatePoolSize was made after PeerConnection has been initialized.
|
||||||
|
#[error("ice candidate pool size cannot be modified")]
|
||||||
|
ErrModifyingICECandidatePoolSize,
|
||||||
|
|
||||||
|
/// ErrStringSizeLimit indicates that the character size limit of string is
|
||||||
|
/// exceeded. The limit is hardcoded to 65535 according to specifications.
|
||||||
|
#[error("data channel label exceeds size limit")]
|
||||||
|
ErrStringSizeLimit,
|
||||||
|
|
||||||
|
/// ErrMaxDataChannelID indicates that the maximum number ID that could be
|
||||||
|
/// specified for a data channel has been exceeded.
|
||||||
|
#[error("maximum number ID for datachannel specified")]
|
||||||
|
ErrMaxDataChannelID,
|
||||||
|
|
||||||
|
/// ErrNegotiatedWithoutID indicates that an attempt to create a data channel
|
||||||
|
/// was made while setting the negotiated option to true without providing
|
||||||
|
/// the negotiated channel ID.
|
||||||
|
#[error("negotiated set without channel id")]
|
||||||
|
ErrNegotiatedWithoutID,
|
||||||
|
|
||||||
|
/// ErrRetransmitsOrPacketLifeTime indicates that an attempt to create a data
|
||||||
|
/// channel was made with both options max_packet_life_time and max_retransmits
|
||||||
|
/// set together. Such configuration is not supported by the specification
|
||||||
|
/// and is mutually exclusive.
|
||||||
|
#[error("both max_packet_life_time and max_retransmits was set")]
|
||||||
|
ErrRetransmitsOrPacketLifeTime,
|
||||||
|
|
||||||
|
/// ErrCodecNotFound is returned when a codec search to the Media Engine fails
|
||||||
|
#[error("codec not found")]
|
||||||
|
ErrCodecNotFound,
|
||||||
|
|
||||||
|
/// ErrNoRemoteDescription indicates that an operation was rejected because
|
||||||
|
/// the remote description is not set
|
||||||
|
#[error("remote description is not set")]
|
||||||
|
ErrNoRemoteDescription,
|
||||||
|
|
||||||
|
/// ErrIncorrectSDPSemantics indicates that the PeerConnection was configured to
|
||||||
|
/// generate SDP Answers with different SDP Semantics than the received Offer
|
||||||
|
#[error("offer SDP semantics does not match configuration")]
|
||||||
|
ErrIncorrectSDPSemantics,
|
||||||
|
|
||||||
|
/// ErrIncorrectSignalingState indicates that the signaling state of PeerConnection is not correct
|
||||||
|
#[error("operation can not be run in current signaling state")]
|
||||||
|
ErrIncorrectSignalingState,
|
||||||
|
|
||||||
|
/// ErrProtocolTooLarge indicates that value given for a DataChannelInit protocol is
|
||||||
|
/// longer then 65535 bytes
|
||||||
|
#[error("protocol is larger then 65535 bytes")]
|
||||||
|
ErrProtocolTooLarge,
|
||||||
|
|
||||||
|
/// ErrSenderNotCreatedByConnection indicates remove_track was called with a RtpSender not created
|
||||||
|
/// by this PeerConnection
|
||||||
|
#[error("RtpSender not created by this PeerConnection")]
|
||||||
|
ErrSenderNotCreatedByConnection,
|
||||||
|
|
||||||
|
/// ErrSenderInitialTrackIdAlreadySet indicates a second call to
|
||||||
|
/// [`RtpSender::set_initial_track_id`] which is not allowed.
|
||||||
|
#[error("RtpSender's initial_track_id has already been set")]
|
||||||
|
ErrSenderInitialTrackIdAlreadySet,
|
||||||
|
|
||||||
|
/// ErrSessionDescriptionNoFingerprint indicates set_remote_description was called with a SessionDescription that has no
|
||||||
|
/// fingerprint
|
||||||
|
#[error("set_remote_description called with no fingerprint")]
|
||||||
|
ErrSessionDescriptionNoFingerprint,
|
||||||
|
|
||||||
|
/// ErrSessionDescriptionInvalidFingerprint indicates set_remote_description was called with a SessionDescription that
|
||||||
|
/// has an invalid fingerprint
|
||||||
|
#[error("set_remote_description called with an invalid fingerprint")]
|
||||||
|
ErrSessionDescriptionInvalidFingerprint,
|
||||||
|
|
||||||
|
/// ErrSessionDescriptionConflictingFingerprints indicates set_remote_description was called with a SessionDescription that
|
||||||
|
/// has an conflicting fingerprints
|
||||||
|
#[error("set_remote_description called with multiple conflicting fingerprint")]
|
||||||
|
ErrSessionDescriptionConflictingFingerprints,
|
||||||
|
|
||||||
|
/// ErrSessionDescriptionMissingIceUfrag indicates set_remote_description was called with a SessionDescription that
|
||||||
|
/// is missing an ice-ufrag value
|
||||||
|
#[error("set_remote_description called with no ice-ufrag")]
|
||||||
|
ErrSessionDescriptionMissingIceUfrag,
|
||||||
|
|
||||||
|
/// ErrSessionDescriptionMissingIcePwd indicates set_remote_description was called with a SessionDescription that
|
||||||
|
/// is missing an ice-pwd value
|
||||||
|
#[error("set_remote_description called with no ice-pwd")]
|
||||||
|
ErrSessionDescriptionMissingIcePwd,
|
||||||
|
|
||||||
|
/// ErrSessionDescriptionConflictingIceUfrag indicates set_remote_description was called with a SessionDescription that
|
||||||
|
/// contains multiple conflicting ice-ufrag values
|
||||||
|
#[error("set_remote_description called with multiple conflicting ice-ufrag values")]
|
||||||
|
ErrSessionDescriptionConflictingIceUfrag,
|
||||||
|
|
||||||
|
/// ErrSessionDescriptionConflictingIcePwd indicates set_remote_description was called with a SessionDescription that
|
||||||
|
/// contains multiple conflicting ice-pwd values
|
||||||
|
#[error("set_remote_description called with multiple conflicting ice-pwd values")]
|
||||||
|
ErrSessionDescriptionConflictingIcePwd,
|
||||||
|
|
||||||
|
/// ErrNoSRTPProtectionProfile indicates that the DTLS handshake completed and no SRTP Protection Profile was chosen
|
||||||
|
#[error("DTLS Handshake completed and no SRTP Protection Profile was chosen")]
|
||||||
|
ErrNoSRTPProtectionProfile,
|
||||||
|
|
||||||
|
/// ErrFailedToGenerateCertificateFingerprint indicates that we failed to generate the fingerprint used for comparing certificates
|
||||||
|
#[error("failed to generate certificate fingerprint")]
|
||||||
|
ErrFailedToGenerateCertificateFingerprint,
|
||||||
|
|
||||||
|
/// ErrNoCodecsAvailable indicates that operation isn't possible because the MediaEngine has no codecs available
|
||||||
|
#[error("operation failed no codecs are available")]
|
||||||
|
ErrNoCodecsAvailable,
|
||||||
|
|
||||||
|
/// ErrUnsupportedCodec indicates the remote peer doesn't support the requested codec
|
||||||
|
#[error("unable to start track, codec is not supported by remote")]
|
||||||
|
ErrUnsupportedCodec,
|
||||||
|
|
||||||
|
/// ErrSenderWithNoCodecs indicates that a RTPSender was created without any codecs. To send media the MediaEngine needs at
|
||||||
|
/// least one configured codec.
|
||||||
|
#[error("unable to populate media section, RTPSender created with no codecs")]
|
||||||
|
ErrSenderWithNoCodecs,
|
||||||
|
|
||||||
|
/// ErrRTPSenderNewTrackHasIncorrectKind indicates that the new track is of a different kind than the previous/original
|
||||||
|
#[error("new track must be of the same kind as previous")]
|
||||||
|
ErrRTPSenderNewTrackHasIncorrectKind,
|
||||||
|
|
||||||
|
/// ErrRTPSenderDataSent indicates that the sequence number transformer tries to be enabled after the data sending began
|
||||||
|
#[error("Sequence number transformer must be enabled before sending data")]
|
||||||
|
ErrRTPSenderDataSent,
|
||||||
|
|
||||||
|
/// ErrRTPSenderSeqTransEnabled indicates that the sequence number transformer has been already enabled
|
||||||
|
#[error("Sequence number transformer has been already enabled")]
|
||||||
|
ErrRTPSenderSeqTransEnabled,
|
||||||
|
|
||||||
|
/// ErrUnbindFailed indicates that a TrackLocal was not able to be unbind
|
||||||
|
#[error("failed to unbind TrackLocal from PeerConnection")]
|
||||||
|
ErrUnbindFailed,
|
||||||
|
|
||||||
|
/// ErrNoPayloaderForCodec indicates that the requested codec does not have a payloader
|
||||||
|
#[error("the requested codec does not have a payloader")]
|
||||||
|
ErrNoPayloaderForCodec,
|
||||||
|
|
||||||
|
/// ErrRegisterHeaderExtensionInvalidDirection indicates that a extension was registered with different
|
||||||
|
/// directions for two different calls.
|
||||||
|
#[error("a header extension must be registered with the same direction each time")]
|
||||||
|
ErrRegisterHeaderExtensionInvalidDirection,
|
||||||
|
|
||||||
|
/// ErrRegisterHeaderExtensionNoFreeID indicates that there was no extension ID available which
|
||||||
|
/// in turn means that all 15 available id(1 through 14) have been used.
|
||||||
|
#[error("no header extension ID was free to use(this means the maximum of 15 extensions have been registered)")]
|
||||||
|
ErrRegisterHeaderExtensionNoFreeID,
|
||||||
|
|
||||||
|
/// ErrSimulcastProbeOverflow indicates that too many Simulcast probe streams are in flight and the requested SSRC was ignored
|
||||||
|
#[error("simulcast probe limit has been reached, new SSRC has been discarded")]
|
||||||
|
ErrSimulcastProbeOverflow,
|
||||||
|
|
||||||
|
#[error("enable detaching by calling webrtc.DetachDataChannels()")]
|
||||||
|
ErrDetachNotEnabled,
|
||||||
|
#[error("datachannel not opened yet, try calling Detach from OnOpen")]
|
||||||
|
ErrDetachBeforeOpened,
|
||||||
|
#[error("the DTLS transport has not started yet")]
|
||||||
|
ErrDtlsTransportNotStarted,
|
||||||
|
#[error("failed extracting keys from DTLS for SRTP")]
|
||||||
|
ErrDtlsKeyExtractionFailed,
|
||||||
|
#[error("failed to start SRTP")]
|
||||||
|
ErrFailedToStartSRTP,
|
||||||
|
#[error("failed to start SRTCP")]
|
||||||
|
ErrFailedToStartSRTCP,
|
||||||
|
#[error("attempted to start DTLSTransport that is not in new state")]
|
||||||
|
ErrInvalidDTLSStart,
|
||||||
|
#[error("peer didn't provide certificate via DTLS")]
|
||||||
|
ErrNoRemoteCertificate,
|
||||||
|
#[error("identity provider is not implemented")]
|
||||||
|
ErrIdentityProviderNotImplemented,
|
||||||
|
#[error("remote certificate does not match any fingerprint")]
|
||||||
|
ErrNoMatchingCertificateFingerprint,
|
||||||
|
#[error("unsupported fingerprint algorithm")]
|
||||||
|
ErrUnsupportedFingerprintAlgorithm,
|
||||||
|
#[error("ICE connection not started")]
|
||||||
|
ErrICEConnectionNotStarted,
|
||||||
|
#[error("unknown candidate type")]
|
||||||
|
ErrICECandidateTypeUnknown,
|
||||||
|
#[error("cannot convert ice.CandidateType into webrtc.ICECandidateType, invalid type")]
|
||||||
|
ErrICEInvalidConvertCandidateType,
|
||||||
|
#[error("ICEAgent does not exist")]
|
||||||
|
ErrICEAgentNotExist,
|
||||||
|
#[error("unable to convert ICE candidates to ICECandidates")]
|
||||||
|
ErrICECandidatesConversionFailed,
|
||||||
|
#[error("unknown ICE Role")]
|
||||||
|
ErrICERoleUnknown,
|
||||||
|
#[error("unknown protocol")]
|
||||||
|
ErrICEProtocolUnknown,
|
||||||
|
#[error("gatherer not started")]
|
||||||
|
ErrICEGathererNotStarted,
|
||||||
|
#[error("unknown network type")]
|
||||||
|
ErrNetworkTypeUnknown,
|
||||||
|
#[error("new sdp does not match previous offer")]
|
||||||
|
ErrSDPDoesNotMatchOffer,
|
||||||
|
#[error("new sdp does not match previous answer")]
|
||||||
|
ErrSDPDoesNotMatchAnswer,
|
||||||
|
#[error("provided value is not a valid enum value of type SDPType")]
|
||||||
|
ErrPeerConnSDPTypeInvalidValue,
|
||||||
|
#[error("invalid state change op")]
|
||||||
|
ErrPeerConnStateChangeInvalid,
|
||||||
|
#[error("unhandled state change op")]
|
||||||
|
ErrPeerConnStateChangeUnhandled,
|
||||||
|
#[error("invalid SDP type supplied to SetLocalDescription()")]
|
||||||
|
ErrPeerConnSDPTypeInvalidValueSetLocalDescription,
|
||||||
|
#[error("remoteDescription contained media section without mid value")]
|
||||||
|
ErrPeerConnRemoteDescriptionWithoutMidValue,
|
||||||
|
#[error("remoteDescription has not been set yet")]
|
||||||
|
ErrPeerConnRemoteDescriptionNil,
|
||||||
|
#[error("single media section has an explicit SSRC")]
|
||||||
|
ErrPeerConnSingleMediaSectionHasExplicitSSRC,
|
||||||
|
#[error("could not add transceiver for remote SSRC")]
|
||||||
|
ErrPeerConnRemoteSSRCAddTransceiver,
|
||||||
|
#[error("mid RTP Extensions required for Simulcast")]
|
||||||
|
ErrPeerConnSimulcastMidRTPExtensionRequired,
|
||||||
|
#[error("stream id RTP Extensions required for Simulcast")]
|
||||||
|
ErrPeerConnSimulcastStreamIDRTPExtensionRequired,
|
||||||
|
#[error("incoming SSRC failed Simulcast probing")]
|
||||||
|
ErrPeerConnSimulcastIncomingSSRCFailed,
|
||||||
|
#[error("failed collecting stats")]
|
||||||
|
ErrPeerConnStatsCollectionFailed,
|
||||||
|
#[error("add_transceiver_from_kind only accepts one RTPTransceiverInit")]
|
||||||
|
ErrPeerConnAddTransceiverFromKindOnlyAcceptsOne,
|
||||||
|
#[error("add_transceiver_from_track only accepts one RTPTransceiverInit")]
|
||||||
|
ErrPeerConnAddTransceiverFromTrackOnlyAcceptsOne,
|
||||||
|
#[error("add_transceiver_from_kind currently only supports recvonly")]
|
||||||
|
ErrPeerConnAddTransceiverFromKindSupport,
|
||||||
|
#[error("add_transceiver_from_track currently only supports sendonly and sendrecv")]
|
||||||
|
ErrPeerConnAddTransceiverFromTrackSupport,
|
||||||
|
#[error("TODO set_identity_provider")]
|
||||||
|
ErrPeerConnSetIdentityProviderNotImplemented,
|
||||||
|
#[error("write_rtcp failed to open write_stream")]
|
||||||
|
ErrPeerConnWriteRTCPOpenWriteStream,
|
||||||
|
#[error("cannot find transceiver with mid")]
|
||||||
|
ErrPeerConnTransceiverMidNil,
|
||||||
|
#[error("DTLSTransport must not be nil")]
|
||||||
|
ErrRTPReceiverDTLSTransportNil,
|
||||||
|
#[error("Receive has already been called")]
|
||||||
|
ErrRTPReceiverReceiveAlreadyCalled,
|
||||||
|
#[error("unable to find stream for Track with SSRC")]
|
||||||
|
ErrRTPReceiverWithSSRCTrackStreamNotFound,
|
||||||
|
#[error("no trackStreams found for SSRC")]
|
||||||
|
ErrRTPReceiverForSSRCTrackStreamNotFound,
|
||||||
|
#[error("no trackStreams found for RID")]
|
||||||
|
ErrRTPReceiverForRIDTrackStreamNotFound,
|
||||||
|
#[error("invalid RTP Receiver transition from {from} to {to}")]
|
||||||
|
ErrRTPReceiverStateChangeInvalid {
|
||||||
|
from: rtp_receiver::State,
|
||||||
|
to: rtp_receiver::State,
|
||||||
|
},
|
||||||
|
#[error("Track must not be nil")]
|
||||||
|
ErrRTPSenderTrackNil,
|
||||||
|
#[error("RTPSender must not be nil")]
|
||||||
|
ErrRTPSenderNil,
|
||||||
|
#[error("RTPReceiver must not be nil")]
|
||||||
|
ErrRTPReceiverNil,
|
||||||
|
#[error("DTLSTransport must not be nil")]
|
||||||
|
ErrRTPSenderDTLSTransportNil,
|
||||||
|
#[error("Send has already been called")]
|
||||||
|
ErrRTPSenderSendAlreadyCalled,
|
||||||
|
#[error("errRTPSenderTrackNil")]
|
||||||
|
ErrRTPTransceiverCannotChangeMid,
|
||||||
|
#[error("invalid state change in RTPTransceiver.setSending")]
|
||||||
|
ErrRTPTransceiverSetSendingInvalidState,
|
||||||
|
#[error("unsupported codec type by this transceiver")]
|
||||||
|
ErrRTPTransceiverCodecUnsupported,
|
||||||
|
#[error("DTLS not established")]
|
||||||
|
ErrSCTPTransportDTLS,
|
||||||
|
#[error("add_transceiver_sdp() called with 0 transceivers")]
|
||||||
|
ErrSDPZeroTransceivers,
|
||||||
|
#[error("invalid Media Section. Media + DataChannel both enabled")]
|
||||||
|
ErrSDPMediaSectionMediaDataChanInvalid,
|
||||||
|
#[error(
|
||||||
|
"invalid Media Section. Can not have multiple tracks in one MediaSection in UnifiedPlan"
|
||||||
|
)]
|
||||||
|
ErrSDPMediaSectionMultipleTrackInvalid,
|
||||||
|
#[error("set_answering_dtlsrole must DTLSRoleClient or DTLSRoleServer")]
|
||||||
|
ErrSettingEngineSetAnsweringDTLSRole,
|
||||||
|
#[error("can't rollback from stable state")]
|
||||||
|
ErrSignalingStateCannotRollback,
|
||||||
|
#[error(
|
||||||
|
"invalid proposed signaling state transition from {} applying {} {}",
|
||||||
|
from,
|
||||||
|
if *is_local { "local" } else { "remote" },
|
||||||
|
applying
|
||||||
|
)]
|
||||||
|
ErrSignalingStateProposedTransitionInvalid {
|
||||||
|
from: RTCSignalingState,
|
||||||
|
applying: RTCSdpType,
|
||||||
|
is_local: bool,
|
||||||
|
},
|
||||||
|
#[error("cannot convert to StatsICECandidatePairStateSucceeded invalid ice candidate state")]
|
||||||
|
ErrStatsICECandidateStateInvalid,
|
||||||
|
#[error("ICETransport can only be called in ICETransportStateNew")]
|
||||||
|
ErrICETransportNotInNew,
|
||||||
|
#[error("bad Certificate PEM format")]
|
||||||
|
ErrCertificatePEMFormatError,
|
||||||
|
#[error("SCTP is not established")]
|
||||||
|
ErrSCTPNotEstablished,
|
||||||
|
|
||||||
|
#[error("DataChannel is not opened")]
|
||||||
|
ErrClosedPipe,
|
||||||
|
#[error("Interceptor is not bind")]
|
||||||
|
ErrInterceptorNotBind,
|
||||||
|
#[error("excessive retries in CreateOffer")]
|
||||||
|
ErrExcessiveRetries,
|
||||||
|
|
||||||
|
#[error("not long enough to be a RTP Packet")]
|
||||||
|
ErrRTPTooShort,
|
||||||
|
|
||||||
|
#[error("{0}")]
|
||||||
|
Util(#[from] util::Error),
|
||||||
|
#[error("{0}")]
|
||||||
|
Ice(#[from] ice::Error),
|
||||||
|
#[error("{0}")]
|
||||||
|
Srtp(#[from] srtp::Error),
|
||||||
|
#[error("{0}")]
|
||||||
|
Dtls(#[from] dtls::Error),
|
||||||
|
#[error("{0}")]
|
||||||
|
Data(#[from] data::Error),
|
||||||
|
#[error("{0}")]
|
||||||
|
Sctp(#[from] sctp::Error),
|
||||||
|
#[error("{0}")]
|
||||||
|
Sdp(#[from] sdp::Error),
|
||||||
|
#[error("{0}")]
|
||||||
|
Interceptor(#[from] interceptor::Error),
|
||||||
|
#[error("{0}")]
|
||||||
|
Rtcp(#[from] rtcp::Error),
|
||||||
|
#[error("{0}")]
|
||||||
|
Rtp(#[from] rtp::Error),
|
||||||
|
|
||||||
|
#[error("utf-8 error: {0}")]
|
||||||
|
Utf8(#[from] FromUtf8Error),
|
||||||
|
#[error("{0}")]
|
||||||
|
RcGen(#[from] RcgenError),
|
||||||
|
#[error("mpsc send: {0}")]
|
||||||
|
MpscSend(String),
|
||||||
|
#[error("parse int: {0}")]
|
||||||
|
ParseInt(#[from] ParseIntError),
|
||||||
|
#[error("parse url: {0}")]
|
||||||
|
ParseUrl(#[from] url::ParseError),
|
||||||
|
|
||||||
|
/// Error parsing a given PEM string.
|
||||||
|
#[error("invalid PEM: {0}")]
|
||||||
|
InvalidPEM(String),
|
||||||
|
|
||||||
|
#[allow(non_camel_case_types)]
|
||||||
|
#[error("{0}")]
|
||||||
|
new(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type OnErrorHdlrFn =
|
||||||
|
Box<dyn (FnMut(Error) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>) + Send + Sync>;
|
||||||
|
|
||||||
|
// Because Tokio SendError is parameterized, we sadly lose the backtrace.
|
||||||
|
impl<T> From<MpscSendError<T>> for Error {
|
||||||
|
fn from(e: MpscSendError<T>) -> Self {
|
||||||
|
Error::MpscSend(e.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Error> for interceptor::Error {
|
||||||
|
fn from(e: Error) -> Self {
|
||||||
|
// this is a bit lol, but we do preserve the stack trace
|
||||||
|
interceptor::Error::Util(util::Error::from_std(e))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq<ice::Error> for Error {
|
||||||
|
fn eq(&self, other: &ice::Error) -> bool {
|
||||||
|
if let Error::Ice(e) = self {
|
||||||
|
return e == other;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// flatten_errs flattens multiple errors into one
|
||||||
|
pub fn flatten_errs(errs: Vec<impl Into<Error>>) -> Result<()> {
|
||||||
|
if errs.is_empty() {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
let errs_strs: Vec<String> = errs.into_iter().map(|e| e.into().to_string()).collect();
|
||||||
|
Err(Error::new(errs_strs.join("\n")))
|
||||||
|
}
|
||||||
|
}
|
||||||
+219
@@ -0,0 +1,219 @@
|
|||||||
|
use std::fmt;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use ice::candidate::candidate_base::CandidateBaseConfig;
|
||||||
|
use ice::candidate::candidate_host::CandidateHostConfig;
|
||||||
|
use ice::candidate::candidate_peer_reflexive::CandidatePeerReflexiveConfig;
|
||||||
|
use ice::candidate::candidate_relay::CandidateRelayConfig;
|
||||||
|
use ice::candidate::candidate_server_reflexive::CandidateServerReflexiveConfig;
|
||||||
|
use ice::candidate::Candidate;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::error::{Error, Result};
|
||||||
|
use crate::ice_transport::ice_candidate_type::RTCIceCandidateType;
|
||||||
|
use crate::ice_transport::ice_protocol::RTCIceProtocol;
|
||||||
|
|
||||||
|
/// ICECandidate represents a ice candidate
|
||||||
|
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
pub struct RTCIceCandidate {
|
||||||
|
pub stats_id: String,
|
||||||
|
pub foundation: String,
|
||||||
|
pub priority: u32,
|
||||||
|
pub address: String,
|
||||||
|
pub protocol: RTCIceProtocol,
|
||||||
|
pub port: u16,
|
||||||
|
pub typ: RTCIceCandidateType,
|
||||||
|
pub component: u16,
|
||||||
|
pub related_address: String,
|
||||||
|
pub related_port: u16,
|
||||||
|
pub tcp_type: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Conversion for ice_candidates
|
||||||
|
pub(crate) fn rtc_ice_candidates_from_ice_candidates(
|
||||||
|
ice_candidates: &[Arc<dyn Candidate + Send + Sync>],
|
||||||
|
) -> Vec<RTCIceCandidate> {
|
||||||
|
ice_candidates.iter().map(|c| c.into()).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&Arc<dyn Candidate + Send + Sync>> for RTCIceCandidate {
|
||||||
|
fn from(c: &Arc<dyn Candidate + Send + Sync>) -> Self {
|
||||||
|
let typ: RTCIceCandidateType = c.candidate_type().into();
|
||||||
|
let protocol = RTCIceProtocol::from(c.network_type().network_short().as_str());
|
||||||
|
let (related_address, related_port) = if let Some(ra) = c.related_address() {
|
||||||
|
(ra.address, ra.port)
|
||||||
|
} else {
|
||||||
|
(String::new(), 0)
|
||||||
|
};
|
||||||
|
|
||||||
|
RTCIceCandidate {
|
||||||
|
stats_id: c.id(),
|
||||||
|
foundation: c.foundation(),
|
||||||
|
priority: c.priority(),
|
||||||
|
address: c.address(),
|
||||||
|
protocol,
|
||||||
|
port: c.port(),
|
||||||
|
component: c.component(),
|
||||||
|
typ,
|
||||||
|
tcp_type: c.tcp_type().to_string(),
|
||||||
|
related_address,
|
||||||
|
related_port,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RTCIceCandidate {
|
||||||
|
pub(crate) fn to_ice(&self) -> Result<impl Candidate> {
|
||||||
|
let candidate_id = self.stats_id.clone();
|
||||||
|
let c = match self.typ {
|
||||||
|
RTCIceCandidateType::Host => {
|
||||||
|
let config = CandidateHostConfig {
|
||||||
|
base_config: CandidateBaseConfig {
|
||||||
|
candidate_id,
|
||||||
|
network: self.protocol.to_string(),
|
||||||
|
address: self.address.clone(),
|
||||||
|
port: self.port,
|
||||||
|
component: self.component,
|
||||||
|
//tcp_type: ice.NewTCPType(c.TCPType),
|
||||||
|
foundation: self.foundation.clone(),
|
||||||
|
priority: self.priority,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
config.new_candidate_host()?
|
||||||
|
}
|
||||||
|
RTCIceCandidateType::Srflx => {
|
||||||
|
let config = CandidateServerReflexiveConfig {
|
||||||
|
base_config: CandidateBaseConfig {
|
||||||
|
candidate_id,
|
||||||
|
network: self.protocol.to_string(),
|
||||||
|
address: self.address.clone(),
|
||||||
|
port: self.port,
|
||||||
|
component: self.component,
|
||||||
|
foundation: self.foundation.clone(),
|
||||||
|
priority: self.priority,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
rel_addr: self.related_address.clone(),
|
||||||
|
rel_port: self.related_port,
|
||||||
|
};
|
||||||
|
config.new_candidate_server_reflexive()?
|
||||||
|
}
|
||||||
|
RTCIceCandidateType::Prflx => {
|
||||||
|
let config = CandidatePeerReflexiveConfig {
|
||||||
|
base_config: CandidateBaseConfig {
|
||||||
|
candidate_id,
|
||||||
|
network: self.protocol.to_string(),
|
||||||
|
address: self.address.clone(),
|
||||||
|
port: self.port,
|
||||||
|
component: self.component,
|
||||||
|
foundation: self.foundation.clone(),
|
||||||
|
priority: self.priority,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
rel_addr: self.related_address.clone(),
|
||||||
|
rel_port: self.related_port,
|
||||||
|
};
|
||||||
|
config.new_candidate_peer_reflexive()?
|
||||||
|
}
|
||||||
|
RTCIceCandidateType::Relay => {
|
||||||
|
let config = CandidateRelayConfig {
|
||||||
|
base_config: CandidateBaseConfig {
|
||||||
|
candidate_id,
|
||||||
|
network: self.protocol.to_string(),
|
||||||
|
address: self.address.clone(),
|
||||||
|
port: self.port,
|
||||||
|
component: self.component,
|
||||||
|
foundation: self.foundation.clone(),
|
||||||
|
priority: self.priority,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
rel_addr: self.related_address.clone(),
|
||||||
|
rel_port: self.related_port,
|
||||||
|
relay_client: None, //TODO?
|
||||||
|
};
|
||||||
|
config.new_candidate_relay()?
|
||||||
|
}
|
||||||
|
_ => return Err(Error::ErrICECandidateTypeUnknown),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// to_json returns an ICECandidateInit
|
||||||
|
/// as indicated by the spec <https://w3c.github.io/webrtc-pc/#dom-rtcicecandidate-tojson>
|
||||||
|
pub fn to_json(&self) -> Result<RTCIceCandidateInit> {
|
||||||
|
let candidate = self.to_ice()?;
|
||||||
|
|
||||||
|
Ok(RTCIceCandidateInit {
|
||||||
|
candidate: format!("candidate:{}", candidate.marshal()),
|
||||||
|
sdp_mid: Some("".to_owned()),
|
||||||
|
sdp_mline_index: Some(0u16),
|
||||||
|
username_fragment: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for RTCIceCandidate {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{} {} {}:{}{}",
|
||||||
|
self.protocol, self.typ, self.address, self.port, self.related_address,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// ICECandidateInit is used to serialize ice candidates
|
||||||
|
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct RTCIceCandidateInit {
|
||||||
|
pub candidate: String,
|
||||||
|
pub sdp_mid: Option<String>,
|
||||||
|
#[serde(rename = "sdpMLineIndex")]
|
||||||
|
pub sdp_mline_index: Option<u16>,
|
||||||
|
pub username_fragment: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ice_candidate_serialization() {
|
||||||
|
let tests = vec![
|
||||||
|
(
|
||||||
|
RTCIceCandidateInit {
|
||||||
|
candidate: "candidate:abc123".to_string(),
|
||||||
|
sdp_mid: Some("0".to_string()),
|
||||||
|
sdp_mline_index: Some(0),
|
||||||
|
username_fragment: Some("def".to_string()),
|
||||||
|
},
|
||||||
|
r#"{"candidate":"candidate:abc123","sdpMid":"0","sdpMLineIndex":0,"usernameFragment":"def"}"#,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
RTCIceCandidateInit {
|
||||||
|
candidate: "candidate:abc123".to_string(),
|
||||||
|
sdp_mid: None,
|
||||||
|
sdp_mline_index: None,
|
||||||
|
username_fragment: None,
|
||||||
|
},
|
||||||
|
r#"{"candidate":"candidate:abc123","sdpMid":null,"sdpMLineIndex":null,"usernameFragment":null}"#,
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (candidate_init, expected_string) in tests {
|
||||||
|
let result = serde_json::to_string(&candidate_init);
|
||||||
|
assert!(result.is_ok(), "testCase: marshal err: {result:?}");
|
||||||
|
let candidate_data = result.unwrap();
|
||||||
|
assert_eq!(candidate_data, expected_string, "string is not expected");
|
||||||
|
|
||||||
|
let result = serde_json::from_str::<RTCIceCandidateInit>(&candidate_data);
|
||||||
|
assert!(result.is_ok(), "testCase: unmarshal err: {result:?}");
|
||||||
|
if let Ok(actual_candidate_init) = result {
|
||||||
|
assert_eq!(actual_candidate_init, candidate_init);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
use crate::ice_transport::ice_candidate::*;
|
||||||
|
|
||||||
|
/// ICECandidatePair represents an ICE Candidate pair
|
||||||
|
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct RTCIceCandidatePair {
|
||||||
|
stats_id: String,
|
||||||
|
local: RTCIceCandidate,
|
||||||
|
remote: RTCIceCandidate,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for RTCIceCandidatePair {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "(local) {} <-> (remote) {}", self.local, self.remote)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RTCIceCandidatePair {
|
||||||
|
fn stats_id(local_id: &str, remote_id: &str) -> String {
|
||||||
|
format!("{local_id}-{remote_id}")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// returns an initialized ICECandidatePair
|
||||||
|
/// for the given pair of ICECandidate instances
|
||||||
|
pub fn new(local: RTCIceCandidate, remote: RTCIceCandidate) -> Self {
|
||||||
|
let stats_id = Self::stats_id(&local.stats_id, &remote.stats_id);
|
||||||
|
RTCIceCandidatePair {
|
||||||
|
stats_id,
|
||||||
|
local,
|
||||||
|
remote,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
use ice::candidate::CandidateType;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// ICECandidateType represents the type of the ICE candidate used.
|
||||||
|
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
pub enum RTCIceCandidateType {
|
||||||
|
#[default]
|
||||||
|
Unspecified,
|
||||||
|
|
||||||
|
/// ICECandidateTypeHost indicates that the candidate is of Host type as
|
||||||
|
/// described in <https://tools.ietf.org/html/rfc8445#section-5.1.1.1>. A
|
||||||
|
/// candidate obtained by binding to a specific port from an IP address on
|
||||||
|
/// the host. This includes IP addresses on physical interfaces and logical
|
||||||
|
/// ones, such as ones obtained through VPNs.
|
||||||
|
#[serde(rename = "host")]
|
||||||
|
Host,
|
||||||
|
|
||||||
|
/// ICECandidateTypeSrflx indicates the the candidate is of Server
|
||||||
|
/// Reflexive type as described
|
||||||
|
/// <https://tools.ietf.org/html/rfc8445#section-5.1.1.2>. A candidate type
|
||||||
|
/// whose IP address and port are a binding allocated by a NAT for an ICE
|
||||||
|
/// agent after it sends a packet through the NAT to a server, such as a
|
||||||
|
/// STUN server.
|
||||||
|
#[serde(rename = "srflx")]
|
||||||
|
Srflx,
|
||||||
|
|
||||||
|
/// ICECandidateTypePrflx indicates that the candidate is of Peer
|
||||||
|
/// Reflexive type. A candidate type whose IP address and port are a binding
|
||||||
|
/// allocated by a NAT for an ICE agent after it sends a packet through the
|
||||||
|
/// NAT to its peer.
|
||||||
|
#[serde(rename = "prflx")]
|
||||||
|
Prflx,
|
||||||
|
|
||||||
|
/// ICECandidateTypeRelay indicates the the candidate is of Relay type as
|
||||||
|
/// described in <https://tools.ietf.org/html/rfc8445#section-5.1.1.2>. A
|
||||||
|
/// candidate type obtained from a relay server, such as a TURN server.
|
||||||
|
#[serde(rename = "relay")]
|
||||||
|
Relay,
|
||||||
|
}
|
||||||
|
|
||||||
|
const ICE_CANDIDATE_TYPE_HOST_STR: &str = "host";
|
||||||
|
const ICE_CANDIDATE_TYPE_SRFLX_STR: &str = "srflx";
|
||||||
|
const ICE_CANDIDATE_TYPE_PRFLX_STR: &str = "prflx";
|
||||||
|
const ICE_CANDIDATE_TYPE_RELAY_STR: &str = "relay";
|
||||||
|
|
||||||
|
/// takes a string and converts it into ICECandidateType
|
||||||
|
impl From<&str> for RTCIceCandidateType {
|
||||||
|
fn from(raw: &str) -> Self {
|
||||||
|
match raw {
|
||||||
|
ICE_CANDIDATE_TYPE_HOST_STR => RTCIceCandidateType::Host,
|
||||||
|
ICE_CANDIDATE_TYPE_SRFLX_STR => RTCIceCandidateType::Srflx,
|
||||||
|
ICE_CANDIDATE_TYPE_PRFLX_STR => RTCIceCandidateType::Prflx,
|
||||||
|
ICE_CANDIDATE_TYPE_RELAY_STR => RTCIceCandidateType::Relay,
|
||||||
|
_ => RTCIceCandidateType::Unspecified,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<CandidateType> for RTCIceCandidateType {
|
||||||
|
fn from(candidate_type: CandidateType) -> Self {
|
||||||
|
match candidate_type {
|
||||||
|
CandidateType::Host => RTCIceCandidateType::Host,
|
||||||
|
CandidateType::ServerReflexive => RTCIceCandidateType::Srflx,
|
||||||
|
CandidateType::PeerReflexive => RTCIceCandidateType::Prflx,
|
||||||
|
CandidateType::Relay => RTCIceCandidateType::Relay,
|
||||||
|
_ => RTCIceCandidateType::Unspecified,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for RTCIceCandidateType {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match *self {
|
||||||
|
RTCIceCandidateType::Host => write!(f, "{ICE_CANDIDATE_TYPE_HOST_STR}"),
|
||||||
|
RTCIceCandidateType::Srflx => write!(f, "{ICE_CANDIDATE_TYPE_SRFLX_STR}"),
|
||||||
|
RTCIceCandidateType::Prflx => write!(f, "{ICE_CANDIDATE_TYPE_PRFLX_STR}"),
|
||||||
|
RTCIceCandidateType::Relay => write!(f, "{ICE_CANDIDATE_TYPE_RELAY_STR}"),
|
||||||
|
_ => write!(f, "{}", crate::UNSPECIFIED_STR),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ice_candidate_type() {
|
||||||
|
let tests = vec![
|
||||||
|
("Unspecified", RTCIceCandidateType::Unspecified),
|
||||||
|
("host", RTCIceCandidateType::Host),
|
||||||
|
("srflx", RTCIceCandidateType::Srflx),
|
||||||
|
("prflx", RTCIceCandidateType::Prflx),
|
||||||
|
("relay", RTCIceCandidateType::Relay),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (type_string, expected_type) in tests {
|
||||||
|
let actual = RTCIceCandidateType::from(type_string);
|
||||||
|
assert_eq!(actual, expected_type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ice_candidate_type_string() {
|
||||||
|
let tests = vec![
|
||||||
|
(RTCIceCandidateType::Unspecified, "Unspecified"),
|
||||||
|
(RTCIceCandidateType::Host, "host"),
|
||||||
|
(RTCIceCandidateType::Srflx, "srflx"),
|
||||||
|
(RTCIceCandidateType::Prflx, "prflx"),
|
||||||
|
(RTCIceCandidateType::Relay, "relay"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (ctype, expected_string) in tests {
|
||||||
|
assert_eq!(ctype.to_string(), expected_string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
/// RTCIceConnectionState indicates signaling state of the ICE Connection.
|
||||||
|
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
|
||||||
|
pub enum RTCIceConnectionState {
|
||||||
|
#[default]
|
||||||
|
Unspecified,
|
||||||
|
|
||||||
|
/// ICEConnectionStateNew indicates that any of the ICETransports are
|
||||||
|
/// in the "new" state and none of them are in the "checking", "disconnected"
|
||||||
|
/// or "failed" state, or all ICETransports are in the "closed" state, or
|
||||||
|
/// there are no transports.
|
||||||
|
New,
|
||||||
|
|
||||||
|
/// ICEConnectionStateChecking indicates that any of the ICETransports
|
||||||
|
/// are in the "checking" state and none of them are in the "disconnected"
|
||||||
|
/// or "failed" state.
|
||||||
|
Checking,
|
||||||
|
|
||||||
|
/// ICEConnectionStateConnected indicates that all ICETransports are
|
||||||
|
/// in the "connected", "completed" or "closed" state and at least one of
|
||||||
|
/// them is in the "connected" state.
|
||||||
|
Connected,
|
||||||
|
|
||||||
|
/// ICEConnectionStateCompleted indicates that all ICETransports are
|
||||||
|
/// in the "completed" or "closed" state and at least one of them is in the
|
||||||
|
/// "completed" state.
|
||||||
|
Completed,
|
||||||
|
|
||||||
|
/// ICEConnectionStateDisconnected indicates that any of the
|
||||||
|
/// ICETransports are in the "disconnected" state and none of them are
|
||||||
|
/// in the "failed" state.
|
||||||
|
Disconnected,
|
||||||
|
|
||||||
|
/// ICEConnectionStateFailed indicates that any of the ICETransports
|
||||||
|
/// are in the "failed" state.
|
||||||
|
Failed,
|
||||||
|
|
||||||
|
/// ICEConnectionStateClosed indicates that the PeerConnection's
|
||||||
|
/// isClosed is true.
|
||||||
|
Closed,
|
||||||
|
}
|
||||||
|
|
||||||
|
const ICE_CONNECTION_STATE_NEW_STR: &str = "new";
|
||||||
|
const ICE_CONNECTION_STATE_CHECKING_STR: &str = "checking";
|
||||||
|
const ICE_CONNECTION_STATE_CONNECTED_STR: &str = "connected";
|
||||||
|
const ICE_CONNECTION_STATE_COMPLETED_STR: &str = "completed";
|
||||||
|
const ICE_CONNECTION_STATE_DISCONNECTED_STR: &str = "disconnected";
|
||||||
|
const ICE_CONNECTION_STATE_FAILED_STR: &str = "failed";
|
||||||
|
const ICE_CONNECTION_STATE_CLOSED_STR: &str = "closed";
|
||||||
|
|
||||||
|
/// takes a string and converts it to iceconnection_state
|
||||||
|
impl From<&str> for RTCIceConnectionState {
|
||||||
|
fn from(raw: &str) -> Self {
|
||||||
|
match raw {
|
||||||
|
ICE_CONNECTION_STATE_NEW_STR => RTCIceConnectionState::New,
|
||||||
|
ICE_CONNECTION_STATE_CHECKING_STR => RTCIceConnectionState::Checking,
|
||||||
|
ICE_CONNECTION_STATE_CONNECTED_STR => RTCIceConnectionState::Connected,
|
||||||
|
ICE_CONNECTION_STATE_COMPLETED_STR => RTCIceConnectionState::Completed,
|
||||||
|
ICE_CONNECTION_STATE_DISCONNECTED_STR => RTCIceConnectionState::Disconnected,
|
||||||
|
ICE_CONNECTION_STATE_FAILED_STR => RTCIceConnectionState::Failed,
|
||||||
|
ICE_CONNECTION_STATE_CLOSED_STR => RTCIceConnectionState::Closed,
|
||||||
|
_ => RTCIceConnectionState::Unspecified,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<u8> for RTCIceConnectionState {
|
||||||
|
fn from(v: u8) -> Self {
|
||||||
|
match v {
|
||||||
|
1 => RTCIceConnectionState::New,
|
||||||
|
2 => RTCIceConnectionState::Checking,
|
||||||
|
3 => RTCIceConnectionState::Connected,
|
||||||
|
4 => RTCIceConnectionState::Completed,
|
||||||
|
5 => RTCIceConnectionState::Disconnected,
|
||||||
|
6 => RTCIceConnectionState::Failed,
|
||||||
|
7 => RTCIceConnectionState::Closed,
|
||||||
|
_ => RTCIceConnectionState::Unspecified,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for RTCIceConnectionState {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
let s = match *self {
|
||||||
|
RTCIceConnectionState::New => ICE_CONNECTION_STATE_NEW_STR,
|
||||||
|
RTCIceConnectionState::Checking => ICE_CONNECTION_STATE_CHECKING_STR,
|
||||||
|
RTCIceConnectionState::Connected => ICE_CONNECTION_STATE_CONNECTED_STR,
|
||||||
|
RTCIceConnectionState::Completed => ICE_CONNECTION_STATE_COMPLETED_STR,
|
||||||
|
RTCIceConnectionState::Disconnected => ICE_CONNECTION_STATE_DISCONNECTED_STR,
|
||||||
|
RTCIceConnectionState::Failed => ICE_CONNECTION_STATE_FAILED_STR,
|
||||||
|
RTCIceConnectionState::Closed => ICE_CONNECTION_STATE_CLOSED_STR,
|
||||||
|
RTCIceConnectionState::Unspecified => crate::UNSPECIFIED_STR,
|
||||||
|
};
|
||||||
|
write!(f, "{s}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_new_ice_connection_state() {
|
||||||
|
let tests = vec![
|
||||||
|
(crate::UNSPECIFIED_STR, RTCIceConnectionState::Unspecified),
|
||||||
|
("new", RTCIceConnectionState::New),
|
||||||
|
("checking", RTCIceConnectionState::Checking),
|
||||||
|
("connected", RTCIceConnectionState::Connected),
|
||||||
|
("completed", RTCIceConnectionState::Completed),
|
||||||
|
("disconnected", RTCIceConnectionState::Disconnected),
|
||||||
|
("failed", RTCIceConnectionState::Failed),
|
||||||
|
("closed", RTCIceConnectionState::Closed),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (state_string, expected_state) in tests {
|
||||||
|
assert_eq!(
|
||||||
|
RTCIceConnectionState::from(state_string),
|
||||||
|
expected_state,
|
||||||
|
"testCase: {expected_state}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ice_connection_state_string() {
|
||||||
|
let tests = vec![
|
||||||
|
(RTCIceConnectionState::Unspecified, crate::UNSPECIFIED_STR),
|
||||||
|
(RTCIceConnectionState::New, "new"),
|
||||||
|
(RTCIceConnectionState::Checking, "checking"),
|
||||||
|
(RTCIceConnectionState::Connected, "connected"),
|
||||||
|
(RTCIceConnectionState::Completed, "completed"),
|
||||||
|
(RTCIceConnectionState::Disconnected, "disconnected"),
|
||||||
|
(RTCIceConnectionState::Failed, "failed"),
|
||||||
|
(RTCIceConnectionState::Closed, "closed"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (state, expected_string) in tests {
|
||||||
|
assert_eq!(state.to_string(), expected_string)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// ICECredentialType indicates the type of credentials used to connect to
|
||||||
|
/// an ICE server.
|
||||||
|
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
|
||||||
|
pub enum RTCIceCredentialType {
|
||||||
|
#[default]
|
||||||
|
Unspecified,
|
||||||
|
|
||||||
|
/// ICECredential::Password describes username and password based
|
||||||
|
/// credentials as described in <https://tools.ietf.org/html/rfc5389>.
|
||||||
|
Password,
|
||||||
|
|
||||||
|
/// ICECredential::Oauth describes token based credential as described
|
||||||
|
/// in <https://tools.ietf.org/html/rfc7635>.
|
||||||
|
/// Not supported in WebRTC 1.0 spec
|
||||||
|
Oauth,
|
||||||
|
}
|
||||||
|
|
||||||
|
const ICE_CREDENTIAL_TYPE_PASSWORD_STR: &str = "password";
|
||||||
|
const ICE_CREDENTIAL_TYPE_OAUTH_STR: &str = "oauth";
|
||||||
|
|
||||||
|
impl From<&str> for RTCIceCredentialType {
|
||||||
|
fn from(raw: &str) -> Self {
|
||||||
|
match raw {
|
||||||
|
ICE_CREDENTIAL_TYPE_PASSWORD_STR => RTCIceCredentialType::Password,
|
||||||
|
ICE_CREDENTIAL_TYPE_OAUTH_STR => RTCIceCredentialType::Oauth,
|
||||||
|
_ => RTCIceCredentialType::Unspecified,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for RTCIceCredentialType {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match *self {
|
||||||
|
RTCIceCredentialType::Password => write!(f, "{ICE_CREDENTIAL_TYPE_PASSWORD_STR}"),
|
||||||
|
RTCIceCredentialType::Oauth => write!(f, "{ICE_CREDENTIAL_TYPE_OAUTH_STR}"),
|
||||||
|
_ => write!(f, "{}", crate::UNSPECIFIED_STR),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_new_ice_credential_type() {
|
||||||
|
let tests = vec![
|
||||||
|
("Unspecified", RTCIceCredentialType::Unspecified),
|
||||||
|
("password", RTCIceCredentialType::Password),
|
||||||
|
("oauth", RTCIceCredentialType::Oauth),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (ct_str, expected_ct) in tests {
|
||||||
|
assert_eq!(RTCIceCredentialType::from(ct_str), expected_ct);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ice_credential_type_string() {
|
||||||
|
let tests = vec![
|
||||||
|
(RTCIceCredentialType::Unspecified, "Unspecified"),
|
||||||
|
(RTCIceCredentialType::Password, "password"),
|
||||||
|
(RTCIceCredentialType::Oauth, "oauth"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (ct, expected_string) in tests {
|
||||||
|
assert_eq!(ct.to_string(), expected_string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+409
@@ -0,0 +1,409 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::sync::atomic::{AtomicU8, Ordering};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use arc_swap::ArcSwapOption;
|
||||||
|
use ice::agent::Agent;
|
||||||
|
use ice::candidate::{Candidate, CandidateType};
|
||||||
|
use ice::url::Url;
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
|
use crate::api::setting_engine::SettingEngine;
|
||||||
|
use crate::error::{Error, Result};
|
||||||
|
use crate::ice_transport::ice_candidate::*;
|
||||||
|
use crate::ice_transport::ice_candidate_type::RTCIceCandidateType;
|
||||||
|
use crate::ice_transport::ice_gatherer_state::RTCIceGathererState;
|
||||||
|
use crate::ice_transport::ice_parameters::RTCIceParameters;
|
||||||
|
use crate::ice_transport::ice_server::RTCIceServer;
|
||||||
|
use crate::peer_connection::policy::ice_transport_policy::RTCIceTransportPolicy;
|
||||||
|
use crate::stats::stats_collector::StatsCollector;
|
||||||
|
use crate::stats::SourceStatsType::*;
|
||||||
|
use crate::stats::{ICECandidatePairStats, StatsReportType};
|
||||||
|
|
||||||
|
/// ICEGatherOptions provides options relating to the gathering of ICE candidates.
|
||||||
|
#[derive(Default, Debug, Clone)]
|
||||||
|
pub struct RTCIceGatherOptions {
|
||||||
|
pub ice_servers: Vec<RTCIceServer>,
|
||||||
|
pub ice_gather_policy: RTCIceTransportPolicy,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type OnLocalCandidateHdlrFn = Box<
|
||||||
|
dyn (FnMut(Option<RTCIceCandidate>) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>)
|
||||||
|
+ Send
|
||||||
|
+ Sync,
|
||||||
|
>;
|
||||||
|
|
||||||
|
pub type OnICEGathererStateChangeHdlrFn = Box<
|
||||||
|
dyn (FnMut(RTCIceGathererState) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>)
|
||||||
|
+ Send
|
||||||
|
+ Sync,
|
||||||
|
>;
|
||||||
|
|
||||||
|
pub type OnGatheringCompleteHdlrFn =
|
||||||
|
Box<dyn (FnMut() -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>) + Send + Sync>;
|
||||||
|
|
||||||
|
/// ICEGatherer gathers local host, server reflexive and relay
|
||||||
|
/// candidates, as well as enabling the retrieval of local Interactive
|
||||||
|
/// Connectivity Establishment (ICE) parameters which can be
|
||||||
|
/// exchanged in signaling.
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct RTCIceGatherer {
|
||||||
|
pub(crate) validated_servers: Vec<Url>,
|
||||||
|
pub(crate) gather_policy: RTCIceTransportPolicy,
|
||||||
|
pub(crate) setting_engine: Arc<SettingEngine>,
|
||||||
|
|
||||||
|
pub(crate) state: Arc<AtomicU8>, //ICEGathererState,
|
||||||
|
pub(crate) agent: Mutex<Option<Arc<ice::agent::Agent>>>,
|
||||||
|
|
||||||
|
pub(crate) on_local_candidate_handler: Arc<ArcSwapOption<Mutex<OnLocalCandidateHdlrFn>>>,
|
||||||
|
pub(crate) on_state_change_handler: Arc<ArcSwapOption<Mutex<OnICEGathererStateChangeHdlrFn>>>,
|
||||||
|
|
||||||
|
// Used for gathering_complete_promise
|
||||||
|
pub(crate) on_gathering_complete_handler: Arc<ArcSwapOption<Mutex<OnGatheringCompleteHdlrFn>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RTCIceGatherer {
|
||||||
|
pub(crate) fn new(
|
||||||
|
validated_servers: Vec<Url>,
|
||||||
|
gather_policy: RTCIceTransportPolicy,
|
||||||
|
setting_engine: Arc<SettingEngine>,
|
||||||
|
) -> Self {
|
||||||
|
RTCIceGatherer {
|
||||||
|
gather_policy,
|
||||||
|
validated_servers,
|
||||||
|
setting_engine,
|
||||||
|
state: Arc::new(AtomicU8::new(RTCIceGathererState::New as u8)),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn create_agent(&self) -> Result<()> {
|
||||||
|
// NOTE: A lock is held for the duration of this function in order to
|
||||||
|
// avoid potential double-agent creations. Care should be taken to
|
||||||
|
// ensure we do not do anything expensive other than the actual agent
|
||||||
|
// creation in this function.
|
||||||
|
let mut agent = self.agent.lock().await;
|
||||||
|
|
||||||
|
if agent.is_some() || self.state() != RTCIceGathererState::New {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut candidate_types = vec![];
|
||||||
|
if self.setting_engine.candidates.ice_lite {
|
||||||
|
candidate_types.push(ice::candidate::CandidateType::Host);
|
||||||
|
} else if self.gather_policy == RTCIceTransportPolicy::Relay {
|
||||||
|
candidate_types.push(ice::candidate::CandidateType::Relay);
|
||||||
|
}
|
||||||
|
|
||||||
|
let nat_1to1_cand_type = match self.setting_engine.candidates.nat_1to1_ip_candidate_type {
|
||||||
|
RTCIceCandidateType::Host => CandidateType::Host,
|
||||||
|
RTCIceCandidateType::Srflx => CandidateType::ServerReflexive,
|
||||||
|
_ => CandidateType::Unspecified,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mdns_mode = self.setting_engine.candidates.multicast_dns_mode;
|
||||||
|
|
||||||
|
let mut config = ice::agent::agent_config::AgentConfig {
|
||||||
|
udp_network: self.setting_engine.udp_network.clone(),
|
||||||
|
lite: self.setting_engine.candidates.ice_lite,
|
||||||
|
urls: self.validated_servers.clone(),
|
||||||
|
disconnected_timeout: self.setting_engine.timeout.ice_disconnected_timeout,
|
||||||
|
failed_timeout: self.setting_engine.timeout.ice_failed_timeout,
|
||||||
|
keepalive_interval: self.setting_engine.timeout.ice_keepalive_interval,
|
||||||
|
candidate_types,
|
||||||
|
host_acceptance_min_wait: self.setting_engine.timeout.ice_host_acceptance_min_wait,
|
||||||
|
srflx_acceptance_min_wait: self.setting_engine.timeout.ice_srflx_acceptance_min_wait,
|
||||||
|
prflx_acceptance_min_wait: self.setting_engine.timeout.ice_prflx_acceptance_min_wait,
|
||||||
|
relay_acceptance_min_wait: self.setting_engine.timeout.ice_relay_acceptance_min_wait,
|
||||||
|
interface_filter: self.setting_engine.candidates.interface_filter.clone(),
|
||||||
|
ip_filter: self.setting_engine.candidates.ip_filter.clone(),
|
||||||
|
nat_1to1_ips: self.setting_engine.candidates.nat_1to1_ips.clone(),
|
||||||
|
nat_1to1_ip_candidate_type: nat_1to1_cand_type,
|
||||||
|
net: self.setting_engine.vnet.clone(),
|
||||||
|
multicast_dns_mode: mdns_mode,
|
||||||
|
multicast_dns_host_name: self
|
||||||
|
.setting_engine
|
||||||
|
.candidates
|
||||||
|
.multicast_dns_host_name
|
||||||
|
.clone(),
|
||||||
|
local_ufrag: self.setting_engine.candidates.username_fragment.clone(),
|
||||||
|
local_pwd: self.setting_engine.candidates.password.clone(),
|
||||||
|
//TODO: TCPMux: self.setting_engine.iceTCPMux,
|
||||||
|
//TODO: ProxyDialer: self.setting_engine.iceProxyDialer,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let requested_network_types = if self.setting_engine.candidates.ice_network_types.is_empty()
|
||||||
|
{
|
||||||
|
ice::network_type::supported_network_types()
|
||||||
|
} else {
|
||||||
|
self.setting_engine.candidates.ice_network_types.clone()
|
||||||
|
};
|
||||||
|
|
||||||
|
config.network_types.extend(requested_network_types);
|
||||||
|
|
||||||
|
*agent = Some(Arc::new(ice::agent::Agent::new(config).await?));
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gather ICE candidates.
|
||||||
|
pub async fn gather(&self) -> Result<()> {
|
||||||
|
self.create_agent().await?;
|
||||||
|
self.set_state(RTCIceGathererState::Gathering).await;
|
||||||
|
|
||||||
|
if let Some(agent) = self.get_agent().await {
|
||||||
|
let state = Arc::clone(&self.state);
|
||||||
|
let on_local_candidate_handler = Arc::clone(&self.on_local_candidate_handler);
|
||||||
|
let on_state_change_handler = Arc::clone(&self.on_state_change_handler);
|
||||||
|
let on_gathering_complete_handler = Arc::clone(&self.on_gathering_complete_handler);
|
||||||
|
|
||||||
|
agent.on_candidate(Box::new(
|
||||||
|
move |candidate: Option<Arc<dyn Candidate + Send + Sync>>| {
|
||||||
|
let state_clone = Arc::clone(&state);
|
||||||
|
let on_local_candidate_handler_clone = Arc::clone(&on_local_candidate_handler);
|
||||||
|
let on_state_change_handler_clone = Arc::clone(&on_state_change_handler);
|
||||||
|
let on_gathering_complete_handler_clone =
|
||||||
|
Arc::clone(&on_gathering_complete_handler);
|
||||||
|
|
||||||
|
Box::pin(async move {
|
||||||
|
if let Some(cand) = candidate {
|
||||||
|
if let Some(handler) = &*on_local_candidate_handler_clone.load() {
|
||||||
|
let mut f = handler.lock().await;
|
||||||
|
f(Some(RTCIceCandidate::from(&cand))).await;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
state_clone
|
||||||
|
.store(RTCIceGathererState::Complete as u8, Ordering::SeqCst);
|
||||||
|
|
||||||
|
if let Some(handler) = &*on_state_change_handler_clone.load() {
|
||||||
|
let mut f = handler.lock().await;
|
||||||
|
f(RTCIceGathererState::Complete).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(handler) = &*on_gathering_complete_handler_clone.load() {
|
||||||
|
let mut f = handler.lock().await;
|
||||||
|
f().await;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(handler) = &*on_local_candidate_handler_clone.load() {
|
||||||
|
let mut f = handler.lock().await;
|
||||||
|
f(None).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
));
|
||||||
|
|
||||||
|
agent.gather_candidates()?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Close prunes all local candidates, and closes the ports.
|
||||||
|
pub async fn close(&self) -> Result<()> {
|
||||||
|
self.set_state(RTCIceGathererState::Closed).await;
|
||||||
|
|
||||||
|
let agent = {
|
||||||
|
let mut agent_opt = self.agent.lock().await;
|
||||||
|
agent_opt.take()
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(agent) = agent {
|
||||||
|
agent.close().await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// get_local_parameters returns the ICE parameters of the ICEGatherer.
|
||||||
|
pub async fn get_local_parameters(&self) -> Result<RTCIceParameters> {
|
||||||
|
self.create_agent().await?;
|
||||||
|
|
||||||
|
let (frag, pwd) = if let Some(agent) = self.get_agent().await {
|
||||||
|
agent.get_local_user_credentials().await
|
||||||
|
} else {
|
||||||
|
return Err(Error::ErrICEAgentNotExist);
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(RTCIceParameters {
|
||||||
|
username_fragment: frag,
|
||||||
|
password: pwd,
|
||||||
|
ice_lite: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// get_local_candidates returns the sequence of valid local candidates associated with the ICEGatherer.
|
||||||
|
pub async fn get_local_candidates(&self) -> Result<Vec<RTCIceCandidate>> {
|
||||||
|
self.create_agent().await?;
|
||||||
|
|
||||||
|
let ice_candidates = if let Some(agent) = self.get_agent().await {
|
||||||
|
agent.get_local_candidates().await?
|
||||||
|
} else {
|
||||||
|
return Err(Error::ErrICEAgentNotExist);
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(rtc_ice_candidates_from_ice_candidates(&ice_candidates))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// on_local_candidate sets an event handler which fires when a new local ICE candidate is available
|
||||||
|
/// Take note that the handler is gonna be called with a nil pointer when gathering is finished.
|
||||||
|
pub fn on_local_candidate(&self, f: OnLocalCandidateHdlrFn) {
|
||||||
|
self.on_local_candidate_handler
|
||||||
|
.store(Some(Arc::new(Mutex::new(f))));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// on_state_change sets an event handler which fires any time the ICEGatherer changes
|
||||||
|
pub fn on_state_change(&self, f: OnICEGathererStateChangeHdlrFn) {
|
||||||
|
self.on_state_change_handler
|
||||||
|
.store(Some(Arc::new(Mutex::new(f))));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// on_gathering_complete sets an event handler which fires any time the ICEGatherer changes
|
||||||
|
pub fn on_gathering_complete(&self, f: OnGatheringCompleteHdlrFn) {
|
||||||
|
self.on_gathering_complete_handler
|
||||||
|
.store(Some(Arc::new(Mutex::new(f))));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// State indicates the current state of the ICE gatherer.
|
||||||
|
pub fn state(&self) -> RTCIceGathererState {
|
||||||
|
self.state.load(Ordering::SeqCst).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn set_state(&self, s: RTCIceGathererState) {
|
||||||
|
self.state.store(s as u8, Ordering::SeqCst);
|
||||||
|
|
||||||
|
if let Some(handler) = &*self.on_state_change_handler.load() {
|
||||||
|
let mut f = handler.lock().await;
|
||||||
|
f(s).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn get_agent(&self) -> Option<Arc<Agent>> {
|
||||||
|
let agent = self.agent.lock().await;
|
||||||
|
agent.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn collect_stats(&self, collector: &StatsCollector) {
|
||||||
|
if let Some(agent) = self.get_agent().await {
|
||||||
|
let mut reports = HashMap::new();
|
||||||
|
|
||||||
|
for stats in agent.get_candidate_pairs_stats().await {
|
||||||
|
let stats: ICECandidatePairStats = stats.into();
|
||||||
|
reports.insert(stats.id.clone(), StatsReportType::CandidatePair(stats));
|
||||||
|
}
|
||||||
|
|
||||||
|
for stats in agent.get_local_candidates_stats().await {
|
||||||
|
reports.insert(
|
||||||
|
stats.id.clone(),
|
||||||
|
StatsReportType::from(LocalCandidate(stats)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
for stats in agent.get_remote_candidates_stats().await {
|
||||||
|
reports.insert(
|
||||||
|
stats.id.clone(),
|
||||||
|
StatsReportType::from(RemoteCandidate(stats)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
collector.merge(reports);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use crate::api::APIBuilder;
|
||||||
|
use crate::ice_transport::ice_gatherer::RTCIceGatherOptions;
|
||||||
|
use crate::ice_transport::ice_server::RTCIceServer;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_new_ice_gatherer_success() -> Result<()> {
|
||||||
|
let opts = RTCIceGatherOptions {
|
||||||
|
ice_servers: vec![RTCIceServer {
|
||||||
|
urls: vec!["stun:stun.l.google.com:19302".to_owned()],
|
||||||
|
..Default::default()
|
||||||
|
}],
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let gatherer = APIBuilder::new().build().new_ice_gatherer(opts)?;
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
gatherer.state(),
|
||||||
|
RTCIceGathererState::New,
|
||||||
|
"Expected gathering state new"
|
||||||
|
);
|
||||||
|
|
||||||
|
let (gather_finished_tx, mut gather_finished_rx) = mpsc::channel::<()>(1);
|
||||||
|
let gather_finished_tx = Arc::new(Mutex::new(Some(gather_finished_tx)));
|
||||||
|
gatherer.on_local_candidate(Box::new(move |c: Option<RTCIceCandidate>| {
|
||||||
|
let gather_finished_tx_clone = Arc::clone(&gather_finished_tx);
|
||||||
|
Box::pin(async move {
|
||||||
|
if c.is_none() {
|
||||||
|
let mut tx = gather_finished_tx_clone.lock().await;
|
||||||
|
tx.take();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}));
|
||||||
|
|
||||||
|
gatherer.gather().await?;
|
||||||
|
|
||||||
|
let _ = gather_finished_rx.recv().await;
|
||||||
|
|
||||||
|
let params = gatherer.get_local_parameters().await?;
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
!params.username_fragment.is_empty() && !params.password.is_empty(),
|
||||||
|
"Empty local username or password frag"
|
||||||
|
);
|
||||||
|
|
||||||
|
let candidates = gatherer.get_local_candidates().await?;
|
||||||
|
|
||||||
|
assert!(!candidates.is_empty(), "No candidates gathered");
|
||||||
|
|
||||||
|
gatherer.close().await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_ice_gather_mdns_candidate_gathering() -> Result<()> {
|
||||||
|
let mut s = SettingEngine::default();
|
||||||
|
s.set_ice_multicast_dns_mode(ice::mdns::MulticastDnsMode::QueryAndGather);
|
||||||
|
|
||||||
|
let gatherer = APIBuilder::new()
|
||||||
|
.with_setting_engine(s)
|
||||||
|
.build()
|
||||||
|
.new_ice_gatherer(RTCIceGatherOptions::default())?;
|
||||||
|
|
||||||
|
let (done_tx, mut done_rx) = mpsc::channel::<()>(1);
|
||||||
|
let done_tx = Arc::new(Mutex::new(Some(done_tx)));
|
||||||
|
gatherer.on_local_candidate(Box::new(move |c: Option<RTCIceCandidate>| {
|
||||||
|
let done_tx_clone = Arc::clone(&done_tx);
|
||||||
|
Box::pin(async move {
|
||||||
|
if let Some(c) = c {
|
||||||
|
if c.address.ends_with(".local") {
|
||||||
|
let mut tx = done_tx_clone.lock().await;
|
||||||
|
tx.take();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}));
|
||||||
|
|
||||||
|
gatherer.gather().await?;
|
||||||
|
|
||||||
|
let _ = done_rx.recv().await;
|
||||||
|
|
||||||
|
gatherer.close().await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
/// ICEGathererState represents the current state of the ICE gatherer.
|
||||||
|
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
|
||||||
|
pub enum RTCIceGathererState {
|
||||||
|
#[default]
|
||||||
|
Unspecified,
|
||||||
|
|
||||||
|
/// ICEGathererStateNew indicates object has been created but
|
||||||
|
/// gather() has not been called.
|
||||||
|
New,
|
||||||
|
|
||||||
|
/// ICEGathererStateGathering indicates gather() has been called,
|
||||||
|
/// and the ICEGatherer is in the process of gathering candidates.
|
||||||
|
Gathering,
|
||||||
|
|
||||||
|
/// ICEGathererStateComplete indicates the ICEGatherer has completed gathering.
|
||||||
|
Complete,
|
||||||
|
|
||||||
|
/// ICEGathererStateClosed indicates the closed state can only be entered
|
||||||
|
/// when the ICEGatherer has been closed intentionally by calling close().
|
||||||
|
Closed,
|
||||||
|
}
|
||||||
|
|
||||||
|
const ICE_GATHERED_STATE_NEW_STR: &str = "new";
|
||||||
|
const ICE_GATHERED_STATE_GATHERING_STR: &str = "gathering";
|
||||||
|
const ICE_GATHERED_STATE_COMPLETE_STR: &str = "complete";
|
||||||
|
const ICE_GATHERED_STATE_CLOSED_STR: &str = "closed";
|
||||||
|
|
||||||
|
impl From<&str> for RTCIceGathererState {
|
||||||
|
fn from(raw: &str) -> Self {
|
||||||
|
match raw {
|
||||||
|
ICE_GATHERED_STATE_NEW_STR => RTCIceGathererState::New,
|
||||||
|
ICE_GATHERED_STATE_GATHERING_STR => RTCIceGathererState::Gathering,
|
||||||
|
ICE_GATHERED_STATE_COMPLETE_STR => RTCIceGathererState::Complete,
|
||||||
|
ICE_GATHERED_STATE_CLOSED_STR => RTCIceGathererState::Closed,
|
||||||
|
_ => RTCIceGathererState::Unspecified,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<u8> for RTCIceGathererState {
|
||||||
|
fn from(v: u8) -> Self {
|
||||||
|
match v {
|
||||||
|
1 => RTCIceGathererState::New,
|
||||||
|
2 => RTCIceGathererState::Gathering,
|
||||||
|
3 => RTCIceGathererState::Complete,
|
||||||
|
4 => RTCIceGathererState::Closed,
|
||||||
|
_ => RTCIceGathererState::Unspecified,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for RTCIceGathererState {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match *self {
|
||||||
|
RTCIceGathererState::New => write!(f, "{ICE_GATHERED_STATE_NEW_STR}"),
|
||||||
|
RTCIceGathererState::Gathering => write!(f, "{ICE_GATHERED_STATE_GATHERING_STR}"),
|
||||||
|
RTCIceGathererState::Complete => {
|
||||||
|
write!(f, "{ICE_GATHERED_STATE_COMPLETE_STR}")
|
||||||
|
}
|
||||||
|
RTCIceGathererState::Closed => {
|
||||||
|
write!(f, "{ICE_GATHERED_STATE_CLOSED_STR}")
|
||||||
|
}
|
||||||
|
_ => write!(f, "{}", crate::UNSPECIFIED_STR),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ice_gatherer_state_string() {
|
||||||
|
let tests = vec![
|
||||||
|
(RTCIceGathererState::Unspecified, "Unspecified"),
|
||||||
|
(RTCIceGathererState::New, "new"),
|
||||||
|
(RTCIceGathererState::Gathering, "gathering"),
|
||||||
|
(RTCIceGathererState::Complete, "complete"),
|
||||||
|
(RTCIceGathererState::Closed, "closed"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (state, expected_string) in tests {
|
||||||
|
assert_eq!(state.to_string(), expected_string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
/// ICEGatheringState describes the state of the candidate gathering process.
|
||||||
|
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
|
||||||
|
pub enum RTCIceGatheringState {
|
||||||
|
#[default]
|
||||||
|
Unspecified,
|
||||||
|
|
||||||
|
/// ICEGatheringStateNew indicates that any of the ICETransports are
|
||||||
|
/// in the "new" gathering state and none of the transports are in the
|
||||||
|
/// "gathering" state, or there are no transports.
|
||||||
|
New,
|
||||||
|
|
||||||
|
/// ICEGatheringStateGathering indicates that any of the ICETransports
|
||||||
|
/// are in the "gathering" state.
|
||||||
|
Gathering,
|
||||||
|
|
||||||
|
/// ICEGatheringStateComplete indicates that at least one ICETransport
|
||||||
|
/// exists, and all ICETransports are in the "completed" gathering state.
|
||||||
|
Complete,
|
||||||
|
}
|
||||||
|
|
||||||
|
const ICE_GATHERING_STATE_NEW_STR: &str = "new";
|
||||||
|
const ICE_GATHERING_STATE_GATHERING_STR: &str = "gathering";
|
||||||
|
const ICE_GATHERING_STATE_COMPLETE_STR: &str = "complete";
|
||||||
|
|
||||||
|
/// takes a string and converts it to ICEGatheringState
|
||||||
|
impl From<&str> for RTCIceGatheringState {
|
||||||
|
fn from(raw: &str) -> Self {
|
||||||
|
match raw {
|
||||||
|
ICE_GATHERING_STATE_NEW_STR => RTCIceGatheringState::New,
|
||||||
|
ICE_GATHERING_STATE_GATHERING_STR => RTCIceGatheringState::Gathering,
|
||||||
|
ICE_GATHERING_STATE_COMPLETE_STR => RTCIceGatheringState::Complete,
|
||||||
|
_ => RTCIceGatheringState::Unspecified,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for RTCIceGatheringState {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match *self {
|
||||||
|
RTCIceGatheringState::New => write!(f, "{ICE_GATHERING_STATE_NEW_STR}"),
|
||||||
|
RTCIceGatheringState::Gathering => write!(f, "{ICE_GATHERING_STATE_GATHERING_STR}"),
|
||||||
|
RTCIceGatheringState::Complete => {
|
||||||
|
write!(f, "{ICE_GATHERING_STATE_COMPLETE_STR}")
|
||||||
|
}
|
||||||
|
_ => write!(f, "{}", crate::UNSPECIFIED_STR),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_new_ice_gathering_state() {
|
||||||
|
let tests = vec![
|
||||||
|
("Unspecified", RTCIceGatheringState::Unspecified),
|
||||||
|
("new", RTCIceGatheringState::New),
|
||||||
|
("gathering", RTCIceGatheringState::Gathering),
|
||||||
|
("complete", RTCIceGatheringState::Complete),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (state_string, expected_state) in tests {
|
||||||
|
assert_eq!(RTCIceGatheringState::from(state_string), expected_state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ice_gathering_state_string() {
|
||||||
|
let tests = vec![
|
||||||
|
(RTCIceGatheringState::Unspecified, "Unspecified"),
|
||||||
|
(RTCIceGatheringState::New, "new"),
|
||||||
|
(RTCIceGatheringState::Gathering, "gathering"),
|
||||||
|
(RTCIceGatheringState::Complete, "complete"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (state, expected_string) in tests {
|
||||||
|
assert_eq!(state.to_string(), expected_string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// ICEParameters includes the ICE username fragment
|
||||||
|
/// and password and other ICE-related parameters.
|
||||||
|
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
pub struct RTCIceParameters {
|
||||||
|
pub username_fragment: String,
|
||||||
|
pub password: String,
|
||||||
|
pub ice_lite: bool,
|
||||||
|
}
|
||||||
+79
@@ -0,0 +1,79 @@
|
|||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// ICEProtocol indicates the transport protocol type that is used in the
|
||||||
|
/// ice.URL structure.
|
||||||
|
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
pub enum RTCIceProtocol {
|
||||||
|
#[default]
|
||||||
|
Unspecified,
|
||||||
|
|
||||||
|
/// UDP indicates the URL uses a UDP transport.
|
||||||
|
#[serde(rename = "udp")]
|
||||||
|
Udp,
|
||||||
|
|
||||||
|
/// TCP indicates the URL uses a TCP transport.
|
||||||
|
#[serde(rename = "tcp")]
|
||||||
|
Tcp,
|
||||||
|
}
|
||||||
|
|
||||||
|
const ICE_PROTOCOL_UDP_STR: &str = "udp";
|
||||||
|
const ICE_PROTOCOL_TCP_STR: &str = "tcp";
|
||||||
|
|
||||||
|
/// takes a string and converts it to ICEProtocol
|
||||||
|
impl From<&str> for RTCIceProtocol {
|
||||||
|
fn from(raw: &str) -> Self {
|
||||||
|
if raw.to_uppercase() == ICE_PROTOCOL_UDP_STR.to_uppercase() {
|
||||||
|
RTCIceProtocol::Udp
|
||||||
|
} else if raw.to_uppercase() == ICE_PROTOCOL_TCP_STR.to_uppercase() {
|
||||||
|
RTCIceProtocol::Tcp
|
||||||
|
} else {
|
||||||
|
RTCIceProtocol::Unspecified
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for RTCIceProtocol {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match *self {
|
||||||
|
RTCIceProtocol::Udp => write!(f, "{ICE_PROTOCOL_UDP_STR}"),
|
||||||
|
RTCIceProtocol::Tcp => write!(f, "{ICE_PROTOCOL_TCP_STR}"),
|
||||||
|
_ => write!(f, "{}", crate::UNSPECIFIED_STR),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_new_ice_protocol() {
|
||||||
|
let tests = vec![
|
||||||
|
("Unspecified", RTCIceProtocol::Unspecified),
|
||||||
|
("udp", RTCIceProtocol::Udp),
|
||||||
|
("tcp", RTCIceProtocol::Tcp),
|
||||||
|
("UDP", RTCIceProtocol::Udp),
|
||||||
|
("TCP", RTCIceProtocol::Tcp),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (proto_string, expected_proto) in tests {
|
||||||
|
let actual = RTCIceProtocol::from(proto_string);
|
||||||
|
assert_eq!(actual, expected_proto);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ice_protocol_string() {
|
||||||
|
let tests = vec![
|
||||||
|
(RTCIceProtocol::Unspecified, "Unspecified"),
|
||||||
|
(RTCIceProtocol::Udp, "udp"),
|
||||||
|
(RTCIceProtocol::Tcp, "tcp"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (proto, expected_string) in tests {
|
||||||
|
assert_eq!(proto.to_string(), expected_string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loaded 100 of 156 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user