mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-16 08:50:40 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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: Swatinem/rust-cache@v2
|
||||
- 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
|
||||
mkdir -p "$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"
|
||||
sha256sum "$name.tar.gz" > "$name.tar.gz.sha256"
|
||||
- uses: softprops/action-gh-release@v2
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
target/
|
||||
*.log
|
||||
.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
|
||||
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,portal: + libglib2.0-dev libpipewire-0.3-dev
|
||||
```
|
||||
@@ -102,8 +104,11 @@ viewer does not reconnect them automatically.
|
||||
|---|---|
|
||||
| `~/.config/uuremote/config.json` | user_id, device_id, client_id, system_id, machine_guid, token, csrf; optional `sign_key`, `ffmpeg_root`. `$UU_CONFIG` overrides the path |
|
||||
| `~/.config/uuremote/host_settings.json` | persisted 安全 settings: auto_lock, keep_mute, mute_permanent |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` (default) / `x11` / `wayland` / `portal` |
|
||||
| `UU_VIDEO_ENCODER=cpu` | force libx264 instead of NVENC |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` (default) / `x11` / `wayland` / `portal` / `kms` (experimental feature) / `none` (headless: no display; desktop offers ignored; tool peers get a nominal 1920x1080 screen + Allow permissions, which the port-mapping viewer requires) |
|
||||
| `UU_INPUT_BACKEND` | `auto` (uinput on Wayland/portal when `/dev/uinput` is accessible, udev `uaccess` rule in README) / `uinput` / `native`. X11 keeps XTest (held-key autorepeat handling) |
|
||||
| `UU_TERMINAL_SHORTCUTS=0` | disable Ctrl+V → Ctrl+Shift+V in focused X11 terminals (Mac Cmd and Ctrl both arrive as VK_LCONTROL) |
|
||||
| `contrib/virtual-desktop/` | Xvfb + session (`UU_VIRTUAL_SESSION`, `UU_VIRTUAL_BUS=user|private`) for monitorless hosts; serve it with `DISPLAY=:99` |
|
||||
| `UU_VIDEO_ENCODER` | `cpu`: 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_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 |
|
||||
@@ -121,7 +126,7 @@ every 60 s.
|
||||
| `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/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/clipboard*.rs` | text clipboard bridge and protocol |
|
||||
| `src/file_trans.rs` | FileTransferFtp RPC |
|
||||
@@ -172,6 +177,13 @@ started a second full host server instead, so check that a subcommand exists in
|
||||
`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,
|
||||
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.
|
||||
- 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
|
||||
@@ -187,6 +199,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
|
||||
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.
|
||||
- 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.
|
||||
- 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
|
||||
@@ -209,3 +224,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.
|
||||
- Update README.md / README.en.md when user-visible features or limitations change, and add
|
||||
`docs/release-notes/<tag>.md` before tagging a release.
|
||||
|
||||
### VAAPI (opt-in / 显式启用)
|
||||
|
||||
`UU_VIDEO_ENCODER=vaapi` selects Intel/AMD H.264 VAAPI; `UU_VAAPI_DEVICE` defaults to
|
||||
`/dev/dri/renderD128` (render-node permissions required). No automatic VAAPI selection.
|
||||
Both FFmpeg process and `--features inproc-av` upload NV12 frames to a VAAPI frame pool.
|
||||
VBR uses the viewer bitrate preset and CQ-derived minimum QP, no B-frames, async depth 1,
|
||||
AUD and the existing GOP/VFR timing. Unsupported device/driver/initialization falls back to libx264
|
||||
with a warning. Accepted on Intel UHD 770 (iHD) with the default FFmpeg process pipeline and the official Mac
|
||||
viewer (presets, fps, scaling, reconnect; no fallback, ~40% less CPU than libx264). AMD and the `inproc-av`
|
||||
VAAPI path are not hardware-verified.
|
||||
|
||||
### Virtual audio / 虚拟声卡
|
||||
|
||||
The viewer's 静音运行 switch creates the null sink `uu-remote-sink`,
|
||||
moves streams from the original default output and captures `uu-remote-sink.monitor`.
|
||||
New output goes to this sink; streams explicitly pinned to other outputs are untouched.
|
||||
The last desktop session restores the previous default output and unloads the module.
|
||||
`mute_permanent` retains the physical mute policy, but still releases the virtual sink.
|
||||
A private recovery record beside the config and a process lock recover crash leftovers on startup.
|
||||
`pactl` is used when installed (PulseAudio / PipeWire-pulse); plain PipeWire systems without it (default
|
||||
Ubuntu desktop) use `pw-cli`/`wpctl` for the same sink and WirePlumber moves playing streams. Accepted on
|
||||
Ubuntu 24.04 with the official Mac viewer. When routing is unavailable, the existing mute/monitor path remains.
|
||||
Changing the switch restarts only the scoped audio encoder; normal non-muted capture is unchanged.
|
||||
`virtual-audio-probe` temporarily routes output for five seconds and restores it (local diagnostic).
|
||||
|
||||
### Virtual desktop user bus
|
||||
|
||||
The supervisor imports DISPLAY and XAUTHORITY into the linger user bus, waits for authenticated
|
||||
X11 readiness, and keeps its cleanup trap alive. The sample drop-in uses `%t/uu-virtual-99.Xauthority`.
|
||||
Use a dedicated headless account. See the [setup and official Mac acceptance checklist](contrib/virtual-desktop/ACCEPTANCE.md).
|
||||
|
||||
### Arch Linux / Omarchy
|
||||
|
||||
```sh
|
||||
sudo pacman -S --needed base-devel rust pkgconf clang ffmpeg libx11 libxext libxtst libxfixes gtk3 \
|
||||
wayland libxkbcommon wl-clipboard libpipewire glib2 libpulse
|
||||
cargo test --locked --features wayland
|
||||
cargo build --release --features wayland
|
||||
```
|
||||
|
||||
Omarchy uses Hyprland: run in its user session, select `UU_DESKTOP_BACKEND=wayland`,
|
||||
and retain `WAYLAND_DISPLAY` / `XDG_RUNTIME_DIR`. This requires wlr-screencopy and the
|
||||
virtual input protocols supported by Hyprland; other compositors may need `--features wayland,portal`.
|
||||
For uinput, load `uinput` and apply the uaccess rule above; seat ACLs require an active login.
|
||||
For unattended accounts, explicitly restrict `/dev/uinput` access to a dedicated group.
|
||||
This permission can inject into the whole seat, including lock/greeter screens.
|
||||
`UU_INPUT_BACKEND=uinput` requests kernel devices; verify startup actually reports uinput.
|
||||
Optional VAAPI: `intel-media-driver` (modern Intel) or `libva-mesa-driver` (AMD), `libva-utils`
|
||||
for `vainfo`. Optional KMS: `libdrm`. Virtual Xfce: `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
|
||||
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`.
|
||||
Rolling Arch FFmpeg may be newer than supported `inproc-av` (FFmpeg 6.x); use the default process
|
||||
pipeline or build against a separate FFmpeg 6 prefix. Do not downgrade the system FFmpeg.
|
||||
User-bus headless setup and the official Mac checklist: [virtual desktop acceptance](contrib/virtual-desktop/ACCEPTANCE.md).
|
||||
|
||||
### Experimental KMS / 实验后端
|
||||
|
||||
`--features kms` plus explicit `UU_DESKTOP_BACKEND=kms` enables a limited linear-framebuffer
|
||||
prototype; `UU_KMS_DEVICE=/dev/dri/card0`. Requires libdrm headers and deliberately granted
|
||||
`cap_sys_admin+ep`, plus card-node access. It is never automatically selected or granted privileges.
|
||||
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",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "as-raw-xcb-connection"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b"
|
||||
|
||||
[[package]]
|
||||
name = "asn1-rs"
|
||||
version = "0.5.2"
|
||||
@@ -212,6 +218,26 @@ dependencies = [
|
||||
"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]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
@@ -276,7 +302,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6651c9ed80effdc7db0ff72512157f901af5e3549e341e24b1dd4887d836d838"
|
||||
dependencies = [
|
||||
"find-msvc-tools",
|
||||
"shlex",
|
||||
"shlex 2.0.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -291,6 +317,15 @@ dependencies = [
|
||||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cexpr"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
|
||||
dependencies = [
|
||||
"nom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.5"
|
||||
@@ -324,6 +359,17 @@ dependencies = [
|
||||
"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]]
|
||||
name = "colorchoice"
|
||||
version = "1.0.5"
|
||||
@@ -605,6 +651,31 @@ dependencies = [
|
||||
"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]]
|
||||
name = "fiat-crypto"
|
||||
version = "0.2.9"
|
||||
@@ -736,6 +807,16 @@ dependencies = [
|
||||
"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]]
|
||||
name = "getrandom"
|
||||
version = "0.2.17"
|
||||
@@ -773,6 +854,12 @@ dependencies = [
|
||||
"polyval",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "glob"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e4eba85ea1d0a966a983acd07deee566e67395d2d96b6fb39e62b5a833f1eb0b"
|
||||
|
||||
[[package]]
|
||||
name = "group"
|
||||
version = "0.13.0"
|
||||
@@ -784,6 +871,12 @@ dependencies = [
|
||||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.5.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e17592d60ebacc7d5e169f4663c5f84f9161cc90328abcfe8456f41e4dfcb284"
|
||||
|
||||
[[package]]
|
||||
name = "hex"
|
||||
version = "0.4.3"
|
||||
@@ -1111,12 +1204,34 @@ version = "1.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb"
|
||||
|
||||
[[package]]
|
||||
name = "lazycell"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.190"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "litemap"
|
||||
version = "0.8.3"
|
||||
@@ -1265,6 +1380,16 @@ dependencies = [
|
||||
"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]]
|
||||
name = "oid-registry"
|
||||
version = "0.6.1"
|
||||
@@ -1339,6 +1464,12 @@ dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "peeking_take_while"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
|
||||
|
||||
[[package]]
|
||||
name = "pem"
|
||||
version = "3.0.6"
|
||||
@@ -1386,6 +1517,12 @@ dependencies = [
|
||||
"spki",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pkg-config"
|
||||
version = "0.3.34"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548"
|
||||
|
||||
[[package]]
|
||||
name = "polyval"
|
||||
version = "0.6.2"
|
||||
@@ -1472,7 +1609,7 @@ dependencies = [
|
||||
"pin-project-lite",
|
||||
"quinn-proto",
|
||||
"quinn-udp",
|
||||
"rustc-hash",
|
||||
"rustc-hash 2.1.3",
|
||||
"rustls 0.23.45",
|
||||
"socket2 0.6.5",
|
||||
"thiserror 2.0.21",
|
||||
@@ -1493,7 +1630,7 @@ dependencies = [
|
||||
"rand 0.10.3",
|
||||
"rand_pcg",
|
||||
"ring 0.17.14",
|
||||
"rustc-hash",
|
||||
"rustc-hash 2.1.3",
|
||||
"rustls 0.23.45",
|
||||
"rustls-pki-types",
|
||||
"slab",
|
||||
@@ -1740,6 +1877,12 @@ dependencies = [
|
||||
"webrtc-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-hash"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
||||
|
||||
[[package]]
|
||||
name = "rustc-hash"
|
||||
version = "2.1.3"
|
||||
@@ -1764,6 +1907,19 @@ dependencies = [
|
||||
"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]]
|
||||
name = "rustls"
|
||||
version = "0.21.12"
|
||||
@@ -1958,6 +2114,12 @@ dependencies = [
|
||||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shlex"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
||||
|
||||
[[package]]
|
||||
name = "shlex"
|
||||
version = "2.0.1"
|
||||
@@ -2487,12 +2649,13 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "uu-remote-linux"
|
||||
version = "0.1.2"
|
||||
version = "0.1.10"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"bytes",
|
||||
"cc",
|
||||
"env_logger",
|
||||
"ffmpeg-next",
|
||||
"flate2",
|
||||
"futures-util",
|
||||
"hex",
|
||||
@@ -2508,6 +2671,7 @@ dependencies = [
|
||||
"tokio",
|
||||
"tokio-tungstenite",
|
||||
"webrtc",
|
||||
"x11rb",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2521,6 +2685,12 @@ dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.5"
|
||||
@@ -2648,8 +2818,6 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "webrtc"
|
||||
version = "0.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fbdf025f0fa62f4bf252b2fb0cff0a04d3eac2021c440096649e62f4e48553d"
|
||||
dependencies = [
|
||||
"arc-swap",
|
||||
"async-trait",
|
||||
@@ -2962,6 +3130,25 @@ version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "x25519-dalek"
|
||||
version = "2.0.1"
|
||||
|
||||
+10
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "uu-remote-linux"
|
||||
version = "0.1.2"
|
||||
version = "0.1.10"
|
||||
edition = "2021"
|
||||
authors = ["uu-remote-linux contributors"]
|
||||
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]
|
||||
default = []
|
||||
wayland = []
|
||||
# Experimental direct linear framebuffer capture; never enabled in release builds.
|
||||
kms = []
|
||||
portal = []
|
||||
# In-process capture and encoding through FFmpeg 6 libraries (libavcodec/libavdevice dev packages).
|
||||
# Off by default so release binaries do not link the build machine's libav.
|
||||
inproc-av = ["dep:ffmpeg-next", "dep:x11rb"]
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
@@ -37,9 +42,13 @@ nix = { version = "0.29", features = ["term", "process", "fs", "ioctl", "signal"
|
||||
log = "0.4"
|
||||
qrcode = { version = "0.14", default-features = false }
|
||||
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]
|
||||
cc = "1.0"
|
||||
|
||||
[patch.crates-io]
|
||||
webrtc-dtls = { path = "vendor/webrtc-dtls" }
|
||||
# Larger data-channel receive buffer (see vendor/webrtc/src/data_channel/mod.rs).
|
||||
webrtc = { path = "vendor/webrtc" }
|
||||
+132
-7
@@ -33,7 +33,8 @@
|
||||
|---|---|
|
||||
| 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 |
|
||||
| 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) |
|
||||
| Terminal shortcuts | Mac viewers send both Cmd and Ctrl as Ctrl. In a focused terminal window Ctrl+V / Cmd+V become Ctrl+Shift+V (paste); Ctrl+C always interrupts, copy with Cmd+Shift+C (X11) |
|
||||
| Pointer shape | Remote cursor shape is mirrored to the viewer (X11) |
|
||||
| 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 |
|
||||
@@ -44,7 +45,15 @@
|
||||
| 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 |
|
||||
| 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 |
|
||||
| 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
|
||||
|
||||
@@ -61,10 +70,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
|
||||
`journalctl --user -u uuremote-native.service`.
|
||||
|
||||
Wayland limits: single screen, fixed resolution. The portal backend does not support clipboard, screen wake or
|
||||
dynamic resolution yet. On the first connection the desktop asks for "Remote Desktop / Screen Sharing"
|
||||
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; 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.
|
||||
|
||||
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
|
||||
|
||||
| Feature | Status |
|
||||
@@ -76,8 +93,7 @@ permission; the permission is remembered once granted.
|
||||
| Privacy screen on the host | Not implemented |
|
||||
| Microphone (viewer audio played on the host) | Not implemented (needs a virtual sound card) |
|
||||
| Plugins such as the WoW queue notifier | Not supported; the client is told that no plugins exist |
|
||||
| Access before login (greeter / no user logged in) | Not supported; only an already logged-in desktop session can be controlled |
|
||||
| Port mapping | Code exists, not yet accepted |
|
||||
| Access before login (greeter / no user logged in) | The greeter cannot be controlled; without a logged-in user use headless mode or a virtual desktop (see below) |
|
||||
| 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 |
|
||||
|
||||
@@ -149,6 +165,48 @@ 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.
|
||||
|
||||
### 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.
|
||||
|
||||
## 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
|
||||
|
||||
Common environment variables:
|
||||
@@ -156,11 +214,14 @@ Common environment variables:
|
||||
| Variable | Purpose |
|
||||
|---|---|
|
||||
| `UU_CONFIG` | Config file path (default `~/.config/uuremote/config.json`) |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` |
|
||||
| `UU_VIDEO_ENCODER=cpu` | Force CPU encoding |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` / `kms` (experimental feature) / `none` (headless) |
|
||||
| `UU_INPUT_BACKEND` | `auto` (default: uinput on Wayland when accessible) / `uinput` / `native` |
|
||||
| `UU_TERMINAL_SHORTCUTS=0` | Disable the Ctrl+V paste rewrite in terminal windows |
|
||||
| `UU_VIDEO_ENCODER` | `cpu`: 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_AUDIO=0` | Disable sound; `UU_AUDIO_SOURCE` picks the PulseAudio source |
|
||||
| `UU_LOGIN_WINDOW=0` | Do not open the login window automatically when credentials are missing |
|
||||
| `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
|
||||
`~/.config/uuremote/host_settings.json`.
|
||||
@@ -215,3 +276,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.
|
||||
- [webrtc-rs](https://github.com/webrtc-rs/webrtc), FFmpeg, GTK and the other open-source projects this builds on.
|
||||
- Most of the code was written with the help of Claude and Codex.
|
||||
|
||||
### VAAPI (opt-in / 显式启用)
|
||||
|
||||
`UU_VIDEO_ENCODER=vaapi` selects Intel/AMD H.264 VAAPI; `UU_VAAPI_DEVICE` defaults to
|
||||
`/dev/dri/renderD128` (render-node permissions required). No automatic VAAPI selection.
|
||||
Both FFmpeg process and `--features inproc-av` upload NV12 frames to a VAAPI frame pool.
|
||||
VBR uses the viewer bitrate preset and CQ-derived minimum QP, no B-frames, async depth 1,
|
||||
AUD and the existing GOP/VFR timing. Unsupported device/driver/initialization falls back to libx264
|
||||
with a warning. Accepted on Intel UHD 770 (iHD) with the default FFmpeg process pipeline and the official Mac
|
||||
viewer (presets, fps, scaling, reconnect; no fallback, ~40% less CPU than libx264). AMD and the `inproc-av`
|
||||
VAAPI path are not hardware-verified.
|
||||
|
||||
### Virtual audio / 虚拟声卡
|
||||
|
||||
The viewer's 静音运行 switch creates the null sink `uu-remote-sink`,
|
||||
moves streams from the original default output and captures `uu-remote-sink.monitor`.
|
||||
New output goes to this sink; streams explicitly pinned to other outputs are untouched.
|
||||
The last desktop session restores the previous default output and unloads the module.
|
||||
`mute_permanent` retains the physical mute policy, but still releases the virtual sink.
|
||||
A private recovery record beside the config and a process lock recover crash leftovers on startup.
|
||||
`pactl` is used when installed (PulseAudio / PipeWire-pulse); plain PipeWire systems without it (default
|
||||
Ubuntu desktop) use `pw-cli`/`wpctl` for the same sink and WirePlumber moves playing streams. Accepted on
|
||||
Ubuntu 24.04 with the official Mac viewer. When routing is unavailable, the existing mute/monitor path remains.
|
||||
Changing the switch restarts only the scoped audio encoder; normal non-muted capture is unchanged.
|
||||
`virtual-audio-probe` temporarily routes output for five seconds and restores it (local diagnostic).
|
||||
|
||||
### Virtual desktop user bus
|
||||
|
||||
The supervisor imports DISPLAY and XAUTHORITY into the linger user bus, waits for authenticated
|
||||
X11 readiness, and keeps its cleanup trap alive. The sample drop-in uses `%t/uu-virtual-99.Xauthority`.
|
||||
Use a dedicated headless account. See the [setup and official Mac acceptance checklist](contrib/virtual-desktop/ACCEPTANCE.md).
|
||||
|
||||
### Arch Linux / Omarchy
|
||||
|
||||
```sh
|
||||
sudo pacman -S --needed base-devel rust pkgconf clang ffmpeg libx11 libxext libxtst libxfixes gtk3 \
|
||||
wayland libxkbcommon wl-clipboard libpipewire glib2 libpulse
|
||||
cargo test --locked --features wayland
|
||||
cargo build --release --features wayland
|
||||
```
|
||||
|
||||
Omarchy uses Hyprland: run in its user session, select `UU_DESKTOP_BACKEND=wayland`,
|
||||
and retain `WAYLAND_DISPLAY` / `XDG_RUNTIME_DIR`. This requires wlr-screencopy and the
|
||||
virtual input protocols supported by Hyprland; other compositors may need `--features wayland,portal`.
|
||||
For uinput, load `uinput` and apply the uaccess rule above; seat ACLs require an active login.
|
||||
For unattended accounts, explicitly restrict `/dev/uinput` access to a dedicated group.
|
||||
This permission can inject into the whole seat, including lock/greeter screens.
|
||||
`UU_INPUT_BACKEND=uinput` requests kernel devices; verify startup actually reports uinput.
|
||||
Optional VAAPI: `intel-media-driver` (modern Intel) or `libva-mesa-driver` (AMD), `libva-utils`
|
||||
for `vainfo`. Optional KMS: `libdrm`. Virtual Xfce: `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
|
||||
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`.
|
||||
Rolling Arch FFmpeg may be newer than supported `inproc-av` (FFmpeg 6.x); use the default process
|
||||
pipeline or build against a separate FFmpeg 6 prefix. Do not downgrade the system FFmpeg.
|
||||
User-bus headless setup and the official Mac checklist: [virtual desktop acceptance](contrib/virtual-desktop/ACCEPTANCE.md).
|
||||
|
||||
### Experimental KMS / 实验后端
|
||||
|
||||
`--features kms` plus explicit `UU_DESKTOP_BACKEND=kms` enables a limited linear-framebuffer
|
||||
prototype; `UU_KMS_DEVICE=/dev/dri/card0`. Requires libdrm headers and deliberately granted
|
||||
`cap_sys_admin+ep`, plus card-node access. It is never automatically selected or granted privileges.
|
||||
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.
|
||||
@@ -29,7 +29,8 @@
|
||||
|---|---|
|
||||
| 扫码登录 | GTK 登录窗口(或 `login --no-gui` 终端二维码);二维码过期自动刷新;登录凭证失效时服务会自动弹出登录窗口 |
|
||||
| 远程桌面 | H.264 视频,NVIDIA 显卡用 NVENC 硬件编码,否则用 libx264;查看端绘制本地鼠标,不会出现双光标 |
|
||||
| 鼠标 / 键盘 | 绝对/相对移动、点击、滚轮、按键;修复了网络延迟导致的按键连发 |
|
||||
| 鼠标 / 键盘 | 绝对/相对移动、点击、滚轮、按键;修复了网络延迟导致的按键连发;Wayland 下可用 uinput 虚拟键鼠(不依赖键盘布局,见下文) |
|
||||
| 终端快捷键 | Mac 控制端的 Cmd 和 Ctrl 到被控端都是 Ctrl。焦点在终端窗口时,Ctrl+V / Cmd+V 自动转成 Ctrl+Shift+V 粘贴;Ctrl+C 始终是中断,复制请用 Cmd+Shift+C(X11) |
|
||||
| 鼠标指针样式 | 远端指针形状实时同步(X11) |
|
||||
| 剪贴板 | 双向文本同步 |
|
||||
| 画质 / 帧率 / 分辨率 | 自动、原画 20M、超清 8M、高清 2M、自定义;30/60/90/144 帧;「其他分辨率」由编码器缩放,不改动主机显示器 |
|
||||
@@ -40,7 +41,13 @@
|
||||
| 远程结束后自动锁屏 | 设置保存在本机,重连后依然生效 |
|
||||
| 被控端静音运行 | 主机扬声器静音,控制端照常能听到声音;结束后恢复原来的音量设置 |
|
||||
| 系统操作 | 锁屏、显示桌面、任务管理器(系统监视器)、重启、关机 |
|
||||
| 端口映射 | 官方「端口映射」工具:把控制端本地端口转发到 Linux 能访问到的地址(如 127.0.0.1:22);`UU_PORT_MAPPING=0` 可关闭 |
|
||||
| 多次重连 | 每个工具窗口独立连接,互不影响 |
|
||||
| 无头服务器 | 没有显示器也能上线,提供终端、文件传输和端口映射;也可以用 Xvfb 虚拟桌面提供完整远程桌面(见下文) |
|
||||
|
||||
桌面连接建立、结束及服务启动时会清理遗留输入状态;旧连接的迟到事件不能影响新的桌面连接。
|
||||
X11 清理当前已认证显示上的按下状态(包括无法区分来源的实际按键);uinput 仅清理本服务虚拟设备,
|
||||
Portal 仅作用于本服务获授权的会话。文件传输、终端等独立工具连接不触发桌面松键。
|
||||
|
||||
## 支持的 Linux 桌面
|
||||
|
||||
@@ -56,7 +63,9 @@
|
||||
除 GNOME(X11)外,其余桌面目前都是在虚拟机里测试的。如果你在真机上遇到问题,欢迎[提交 issue](../../issues),
|
||||
请附上发行版、桌面环境、显卡型号和 `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 +79,7 @@ Wayland 下的限制:只支持单屏、固定分辨率;portal 后端暂不
|
||||
| 被控端防窥模式 | 未实现 |
|
||||
| 麦克风(控制端声音传到被控端) | 未实现(需要虚拟声卡) |
|
||||
| 魔兽世界排队提醒等插件 | 不支持,会明确告诉客户端「无插件」 |
|
||||
| 登录前(登录界面 / 无人登录)访问 | 不支持,只能控制已登录的桌面会话 |
|
||||
| 端口映射 | 有代码,未经验收 |
|
||||
| 登录前(登录界面 / 无人登录)访问 | 不支持控制登录界面;没有人登录时可以用无头模式或虚拟桌面(见下文) |
|
||||
| 跨运营商 / 受限网络的中继连接 | 已接入官方下发的 ICE 服务器,但复杂网络环境未充分测试 |
|
||||
| 作为控制端(在 Linux 上控制别的电脑) | 不在本项目范围 |
|
||||
|
||||
@@ -142,6 +150,45 @@ 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,因为它能正确处理网络延迟下按住不放的按键。
|
||||
|
||||
## 没有显示器的服务器
|
||||
|
||||
**无头模式**:只要终端、文件传输和端口映射,不需要图形界面。在服务里加 `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 +196,14 @@ journalctl --user -u uuremote-native.service -f # 看到 availability=Some("re
|
||||
| 变量 | 作用 |
|
||||
|---|---|
|
||||
| `UU_CONFIG` | 配置文件路径(默认 `~/.config/uuremote/config.json`) |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` |
|
||||
| `UU_VIDEO_ENCODER=cpu` | 强制使用 CPU 编码 |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` / `kms`(实验性特性) / `none`(无头模式) |
|
||||
| `UU_INPUT_BACKEND` | `auto`(默认,Wayland 下可用时用 uinput)/ `uinput` / `native` |
|
||||
| `UU_TERMINAL_SHORTCUTS=0` | 关闭终端窗口里的 Ctrl+V 粘贴转换 |
|
||||
| `UU_VIDEO_ENCODER` | `cpu` 强制 CPU;`vaapi` 显式启用 Intel/AMD 硬编;`nvenc` 强制 NVENC;未设置时检测到 NVIDIA 驱动才用 NVENC,否则用 CPU |
|
||||
| `UU_FFMPEG_ROOT` | 使用指定目录下的 FFmpeg |
|
||||
| `UU_AUDIO=0` | 关闭声音转发;`UU_AUDIO_SOURCE` 指定 PulseAudio 采集源 |
|
||||
| `UU_LOGIN_WINDOW=0` | 凭证缺失时不自动弹出登录窗口 |
|
||||
| `UU_PORT_MAPPING=0` | 关闭端口映射(对应官方被控端的开关) |
|
||||
|
||||
「远程结束后自动锁屏」「静音运行」等设置保存在 `~/.config/uuremote/host_settings.json`。
|
||||
|
||||
@@ -200,3 +250,58 @@ flowchart LR
|
||||
- 网易 UU 远程团队:做了一个好用的远程工具。
|
||||
- [webrtc-rs](https://github.com/webrtc-rs/webrtc)、FFmpeg、GTK 等开源项目。
|
||||
- 本项目的大部分代码在 Claude 和 Codex 的协助下完成。
|
||||
|
||||
### VAAPI 硬件编码(显式启用)
|
||||
|
||||
设置 `UU_VIDEO_ENCODER=vaapi` 使用 Intel/AMD H.264 VAAPI;`UU_VAAPI_DEVICE` 默认
|
||||
`/dev/dri/renderD128`,运行用户需要 render 节点权限。不会自动选择 VAAPI;设备或驱动初始化
|
||||
失败会明确警告并回退 libx264。外部 FFmpeg 和 `--features inproc-av` 均先转 NV12 再上传硬件帧。
|
||||
VBR 使用控制端码率预设及 CQ 对应的最小 QP,无 B 帧、async depth=1,并保留现有 GOP/VFR 规则。
|
||||
Intel 核显(UHD 770,iHD 驱动)已实机验收:默认的外部 FFmpeg 管线用官方 Mac 控制端测试了各档画质、帧率、缩放和重连,全程硬件编码、无回退,CPU 占用比 libx264 低约 40%。**AMD 显卡和 `--features inproc-av` 管线的 VAAPI 尚未实机验证。**
|
||||
|
||||
### 静音运行与虚拟声卡
|
||||
|
||||
控制端开启「静音运行」后,程序创建虚拟输出 `uu-remote-sink`,保存原默认输出,
|
||||
迁移其播放流并采集 `uu-remote-sink.monitor`。新播放也走虚拟输出;其他输出上的流不迁移。
|
||||
最后一个桌面连接结束后恢复原默认输出并卸载模块;`mute_permanent` 仍保留原物理静音策略。
|
||||
恢复记录保存在配置旁,权限 0600;进程锁避免重复实例抢占,异常退出后下次启动会恢复遗留路由。
|
||||
有 `pactl` 时用它(PulseAudio / PipeWire-pulse);没有 `pactl` 的纯 PipeWire 系统(如默认的 Ubuntu 桌面)
|
||||
自动改用 `pw-cli`/`wpctl` 创建同名虚拟输出,正在播放的声音由 WirePlumber 自动迁移。已在 Ubuntu 24.04 实机上用官方 Mac 控制端验收。
|
||||
路由失败时保留原静音/monitor 方案。开关变化仅重启当前音频编码器,未开启时采集方式保持不变。
|
||||
本地诊断命令 `virtual-audio-probe` 会临时切换输出五秒并恢复。
|
||||
|
||||
### 虚拟桌面的用户总线
|
||||
|
||||
默认 `UU_VIRTUAL_BUS=user`:监督脚本将 DISPLAY/XAUTHORITY 导入 linger 用户总线,等待经过认证的
|
||||
X11 就绪后通知 systemd;退出时清理 Xvfb。示例主机 drop-in 使用 `%t/uu-virtual-99.Xauthority`。
|
||||
请使用没有真实图形会话的专用账号;参见[设置与官方 Mac 验收清单](contrib/virtual-desktop/ACCEPTANCE.md)。
|
||||
|
||||
### Arch Linux / Omarchy
|
||||
|
||||
```sh
|
||||
sudo pacman -S --needed base-devel rust pkgconf clang ffmpeg libx11 libxext libxtst libxfixes gtk3 \
|
||||
wayland libxkbcommon wl-clipboard libpipewire glib2 libpulse
|
||||
cargo test --locked --features wayland
|
||||
cargo build --release --features wayland
|
||||
```
|
||||
|
||||
Omarchy 使用 Hyprland:在其用户会话中运行,设置 `UU_DESKTOP_BACKEND=wayland`,保留
|
||||
`WAYLAND_DISPLAY` 和 `XDG_RUNTIME_DIR`。其他合成器可能需要 `--features wayland,portal`。
|
||||
uinput 需加载内核模块并应用上文 uaccess 规则;无人登录账号需要专用组的受限授权。
|
||||
该权限能向整个 seat(包括锁屏/登录界面)注入输入。设置 `UU_INPUT_BACKEND=uinput` 后,
|
||||
仍须确认启动日志实际选择了 uinput。
|
||||
|
||||
VAAPI 可选包:Intel 新显卡用 `intel-media-driver`,AMD 用 `libva-mesa-driver`,`libva-utils` 提供
|
||||
`vainfo`。KMS 依赖 `libdrm`。虚拟 Xfce 依赖 `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
|
||||
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`。
|
||||
滚动 Arch 的 FFmpeg 可能超出 `inproc-av` 支持的 6.x;请使用默认进程方案或独立的 FFmpeg 6 前缀,
|
||||
不要降级系统 FFmpeg。
|
||||
|
||||
### 实验性 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 行为。
|
||||
@@ -9,6 +9,16 @@ fn main() {
|
||||
|
||||
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
|
||||
if target_os == "linux" {
|
||||
if std::env::var_os("CARGO_FEATURE_KMS").is_some() {
|
||||
println!("cargo:rerun-if-changed=c_engine/kms_engine.c");
|
||||
let output=std::process::Command::new("pkg-config").args(["--cflags","libdrm"]).output().expect("KMS requires pkg-config and libdrm development headers");
|
||||
assert!(output.status.success(), "KMS requires libdrm development headers");
|
||||
let mut build=cc::Build::new();
|
||||
build.file("c_engine/kms_engine.c");
|
||||
for flag in String::from_utf8_lossy(&output.stdout).split_whitespace() { build.flag(flag); }
|
||||
build.compile("uu_kms_engine");
|
||||
println!("cargo:rustc-link-lib=drm");
|
||||
}
|
||||
cc::Build::new()
|
||||
.file("c_engine/x11_engine.c")
|
||||
.file("c_engine/clipboard.c")
|
||||
@@ -62,7 +72,7 @@ fn main() {
|
||||
let mut build = cc::Build::new();
|
||||
build.flag_if_supported("-Wno-unused-parameter"); // protocol listener signatures
|
||||
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");
|
||||
println!("cargo:rerun-if-changed={xml}");
|
||||
for (mode, suffix) in [("client-header", "client-protocol.h"), ("private-code", "protocol.c")] {
|
||||
@@ -80,4 +90,3 @@ fn main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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 <string.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-virtual-pointer-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_seat *seat;
|
||||
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 zwp_virtual_keyboard_manager_v1 *keyboard_manager;
|
||||
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; };
|
||||
static struct output outputs[32];
|
||||
static unsigned output_count, selected;
|
||||
static uint32_t width,height,stride,format,flags;
|
||||
static int done,failed,initialized;
|
||||
static struct wl_buffer *buffer;
|
||||
static void *pixels;
|
||||
static size_t pixel_length;
|
||||
static uint32_t width,height;
|
||||
static int initialized;
|
||||
/* Independent slots let the compositor fill the next frame during delivery. */
|
||||
struct frame {
|
||||
struct zwlr_screencopy_frame_v1 *handle;
|
||||
struct wl_buffer *buffer;
|
||||
void *pixels;
|
||||
size_t length;
|
||||
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) {
|
||||
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_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 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) {
|
||||
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);
|
||||
@@ -90,6 +142,9 @@ static void global(void*d,struct wl_registry*r,uint32_t id,const char*interface,
|
||||
o->scale=1;
|
||||
o->handle=wl_registry_bind(r,id,&wl_output_interface,version<4?version:4);
|
||||
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_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);
|
||||
@@ -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_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 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) {
|
||||
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 ||
|
||||
(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;
|
||||
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);
|
||||
if (fd<0 || ftruncate(fd,(off_t)length)!=0) { if(fd>=0)close(fd); failed=done=1; return; }
|
||||
pixels=mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
|
||||
if (pixels==MAP_FAILED) { pixels=NULL; close(fd); failed=done=1; return; }
|
||||
pixel_length=length;
|
||||
if (fd<0 || ftruncate(fd,(off_t)length)!=0) { if(fd>=0)close(fd); frame_fail(frame); return; }
|
||||
frame->pixels=mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
|
||||
if (frame->pixels==MAP_FAILED) { frame->pixels=NULL; close(fd); frame_fail(frame); return; }
|
||||
frame->length=length;
|
||||
struct wl_shm_pool *pool=wl_shm_create_pool(shm,fd,(int32_t)length);
|
||||
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);
|
||||
/* 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_ready(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t hi,uint32_t lo,uint32_t ns) { done=1; }
|
||||
static void copy_failed(void*d,struct zwlr_screencopy_frame_v1*f) { failed=done=1; }
|
||||
static void copy_flags(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t value) { ((struct frame*)d)->flags=value; }
|
||||
static void copy_ready(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t hi,uint32_t lo,uint32_t ns) {
|
||||
struct frame *frame=d;
|
||||
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_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; }
|
||||
static void copy_dmabuf(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h) {
|
||||
struct frame *frame=d;
|
||||
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 int capture(void) {
|
||||
done=failed=0; flags=0;
|
||||
if(buffer) { wl_buffer_destroy(buffer); buffer=NULL; }
|
||||
static void request_frame(struct frame *frame) {
|
||||
/* 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);
|
||||
zwlr_screencopy_frame_v1_add_listener(f,©_listener,NULL);
|
||||
int result=dispatch_until(&done);
|
||||
zwlr_screencopy_frame_v1_destroy(f);
|
||||
return result==0 && !failed && pixels ? 0:-1;
|
||||
frame->handle=zwlr_screencopy_manager_v1_capture_output(copy_manager,0,outputs[selected].handle);
|
||||
zwlr_screencopy_frame_v1_add_listener(frame->handle,©_listener,frame);
|
||||
}
|
||||
static void reset(void) {
|
||||
if (key_state) xkb_state_unref(key_state);
|
||||
if (keymap) xkb_keymap_unref(keymap);
|
||||
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. */
|
||||
if (display) wl_display_disconnect(display);
|
||||
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;
|
||||
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));
|
||||
}
|
||||
int uu_wl_init(int32_t *w,int32_t *h) {
|
||||
pthread_mutex_lock(&lock);
|
||||
if(initialized) { *w=width; *h=height; pthread_mutex_unlock(&lock); return 0; }
|
||||
int result=-1;
|
||||
const char *dma=getenv("UU_WAYLAND_DMABUF"); dmabuf_requested=dma && !strcmp(dma,"1");
|
||||
display=wl_display_connect(NULL); if (!display) goto end;
|
||||
struct wl_registry *registry=wl_display_get_registry(display);
|
||||
wl_registry_add_listener(registry,®istry_listener,NULL);
|
||||
if(sync_display()!=0 || sync_display()!=0) goto end;
|
||||
dma_init();
|
||||
if(!shm || !seat || !copy_manager || !pointer_manager || !keyboard_manager || !output_count) goto end;
|
||||
const char *name=getenv("UU_WAYLAND_OUTPUT"); selected=0;
|
||||
if(name && *name) {
|
||||
@@ -203,18 +355,41 @@ int uu_wl_wake(void) {
|
||||
}
|
||||
int uu_wl_capture(uint8_t *out,size_t length) {
|
||||
pthread_mutex_lock(&lock);
|
||||
int result=-1;
|
||||
if(initialized && capture()==0 && length==(size_t)width*height*4) {
|
||||
int result=-1,retried=0;
|
||||
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++) {
|
||||
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;
|
||||
memcpy(row,(uint8_t*)pixels+(size_t)source*stride,width*4);
|
||||
if(format==WL_SHM_FORMAT_XBGR8888 || format==WL_SHM_FORMAT_ABGR8888) {
|
||||
memcpy(row,(uint8_t*)frame->pixels+(size_t)source*frame->stride,width*4);
|
||||
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; }
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
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 <X11/Xlib.h>
|
||||
#include <X11/Xutil.h>
|
||||
#include <X11/Xatom.h>
|
||||
#include <strings.h>
|
||||
#include <X11/extensions/XShm.h>
|
||||
#include <X11/extensions/XTest.h>
|
||||
#include <X11/extensions/Xfixes.h>
|
||||
@@ -58,9 +60,12 @@ int x11_init(const char *display_name) {
|
||||
g_display = XOpenDisplay(display_name);
|
||||
} else {
|
||||
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 (!g_display) g_display = XOpenDisplay(":0");
|
||||
if (!g_display) g_display = XOpenDisplay(":1");
|
||||
else {
|
||||
g_display = XOpenDisplay(":0");
|
||||
if (!g_display) g_display = XOpenDisplay(":1");
|
||||
}
|
||||
}
|
||||
if (!g_display) {
|
||||
return -1;
|
||||
@@ -190,6 +195,70 @@ int x11_key_event(int keycode, int is_press) {
|
||||
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() {
|
||||
if (!g_display) return;
|
||||
if (g_use_shm) {
|
||||
|
||||
@@ -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
|
||||
@@ -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,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.**
|
||||
+76
-17
@@ -1,10 +1,13 @@
|
||||
//! 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.
|
||||
//! `UU_AUDIO=0` disables it; `UU_AUDIO_SOURCE` picks another Pulse source.
|
||||
|
||||
use std::io::Read;
|
||||
use std::process::{Child, Command, Stdio};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
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::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);
|
||||
|
||||
pub fn enabled() -> bool { std::env::var("UU_AUDIO").map_or(true, |v| v != "0") }
|
||||
|
||||
struct Encoder(Child);
|
||||
impl Drop for Encoder { fn drop(&mut self) { let _ = self.0.kill(); let _ = self.0.wait(); } }
|
||||
/// Stops one running encoder when dropped: sets the in-process stop flag, or kills the FFmpeg child.
|
||||
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> {
|
||||
let source = std::env::var("UU_AUDIO_SOURCE").unwrap_or_else(|_| "@DEFAULT_MONITOR@".into());
|
||||
fn spawn_encoder(source: &str) -> Result<Child, String> {
|
||||
let mut command = match crate::config::ffmpeg_root() {
|
||||
Some(root) => { let mut c = Command::new(root.join("bin/ffmpeg")); c.env("LD_LIBRARY_PATH", root.join("lib/x86_64-linux-gnu")); c }
|
||||
None => Command::new("ffmpeg"),
|
||||
};
|
||||
command.args(["-hide_banner", "-loglevel", "error", "-nostdin",
|
||||
"-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",
|
||||
"-frame_duration", "20", "-page_duration", "20000", "-flush_packets", "1", "-f", "ogg", "pipe:1"])
|
||||
.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> {
|
||||
let (mut reader, _) = OggReader::new(stdout, false).map_err(|e| format!("Audio Ogg header: {e}"))?;
|
||||
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 {
|
||||
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;
|
||||
while !closed(&pc) {
|
||||
let mut encoder = match spawn_encoder() {
|
||||
Ok(encoder) => encoder,
|
||||
let source = crate::virtual_audio::source();
|
||||
let (tx, mut rx) = tokio::sync::mpsc::channel::<bytes::Bytes>(25);
|
||||
let (stopper, job) = match start(&source, tx) {
|
||||
Ok(started) => started,
|
||||
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");
|
||||
let mut packets = 0u64;
|
||||
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;
|
||||
},
|
||||
_ = 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(encoder); // kills FFmpeg, which ends the blocking reader
|
||||
let result = reader.await.unwrap_or_else(|e| Err(e.to_string()));
|
||||
drop(stopper);
|
||||
let result = job.await.unwrap_or_else(|e| Err(e.to_string()));
|
||||
if closed(&pc) { break; }
|
||||
if crate::virtual_audio::source() != source { failures=0; continue; }
|
||||
log::warn!("Desktop audio stopped after {packets} packets: {:?}", result.err());
|
||||
failures = if packets > 250 { 0 } else { failures + 1 };
|
||||
if failures >= 5 { log::warn!("Desktop audio disabled for this connection"); return; }
|
||||
@@ -90,3 +117,35 @@ pub async fn stream_audio(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStati
|
||||
}
|
||||
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(()) }
|
||||
}
|
||||
+13
-3
@@ -46,6 +46,7 @@ pub struct ScreenCapture {
|
||||
pub init_error: Option<String>,
|
||||
pub portal: Option<std::sync::Arc<crate::portal::PortalSession>>,
|
||||
pub display: String,
|
||||
kms: Option<crate::kms::Capture>,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
buffer: Mutex<Vec<u8>>,
|
||||
@@ -54,15 +55,21 @@ pub struct ScreenCapture {
|
||||
impl ScreenCapture {
|
||||
pub fn new() -> Result<Self, String> {
|
||||
let backend=crate::desktop_backend::detect()?;
|
||||
if backend == crate::desktop_backend::DesktopBackend::Kms {
|
||||
let kms=crate::kms::Capture::open()?;
|
||||
let (width,height)=(kms.width,kms.height);
|
||||
return Ok(Self { backend,init_error:None,portal:None,display:"kms:linear".into(),width,height,
|
||||
buffer:Mutex::new(vec![0;width as usize*height as usize*4]),kms:Some(kms) });
|
||||
}
|
||||
if backend == crate::desktop_backend::DesktopBackend::Portal {
|
||||
let session=std::sync::Arc::new(crate::portal::PortalSession::open()?);
|
||||
let (width,height)=(session.width,session.height);
|
||||
return Ok(Self { backend,init_error:None,portal:Some(session),display:"wayland:portal".into(),width,height,
|
||||
return Ok(Self { backend,init_error:None,portal:Some(session),kms:None,display:"wayland:portal".into(),width,height,
|
||||
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
|
||||
}
|
||||
if backend == crate::desktop_backend::DesktopBackend::Wayland {
|
||||
let (width,height)=crate::wayland::init()?;
|
||||
return Ok(Self { backend, init_error:None, portal:None, display:"wayland:native".into(), width,height,
|
||||
return Ok(Self { backend, init_error:None, portal:None, kms:None, display:"wayland:native".into(), width,height,
|
||||
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
|
||||
}
|
||||
let ret = unsafe { x11_init(std::ptr::null()) };
|
||||
@@ -90,6 +97,7 @@ impl ScreenCapture {
|
||||
backend,
|
||||
init_error:None,
|
||||
portal:None,
|
||||
kms:None,
|
||||
display,
|
||||
width: w as u32,
|
||||
height: h as u32,
|
||||
@@ -100,13 +108,14 @@ impl ScreenCapture {
|
||||
/// Metadata for non-desktop tools. Never advertise a usable fake screen.
|
||||
pub fn unavailable(error:String)->Self {
|
||||
Self { backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11),
|
||||
init_error:Some(error), portal:None, display:String::new(), width:0,height:0,buffer:Mutex::new(Vec::new()) }
|
||||
init_error:Some(error), portal:None, kms:None, display:String::new(), width:0,height:0,buffer:Mutex::new(Vec::new()) }
|
||||
}
|
||||
|
||||
/// 截取一帧 BGRA 图像
|
||||
pub fn capture_frame(&self) -> Result<Vec<u8>, String> {
|
||||
if let Some(error)=&self.init_error { return Err(error.clone()); }
|
||||
let mut buf = self.buffer.lock().unwrap();
|
||||
if let Some(kms)=&self.kms { kms.capture(&mut buf)?; return Ok(buf.clone()); }
|
||||
if let Some(session)=&self.portal { session.capture(&mut buf)?; return Ok(buf.clone()); }
|
||||
if self.backend == crate::desktop_backend::DesktopBackend::Wayland {
|
||||
crate::wayland::capture(&mut buf)?;
|
||||
@@ -132,6 +141,7 @@ impl Drop for ScreenCapture {
|
||||
|
||||
/// Keep the display visible only while a peer is streaming; never unlock it.
|
||||
pub fn keep_display_awake() -> Result<(), String> {
|
||||
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Kms { return Ok(()); }
|
||||
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Portal { return Ok(()); }
|
||||
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Wayland {
|
||||
return crate::wayland::wake();
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ extern "C" {
|
||||
|
||||
pub fn start(display: String) -> ClipboardBridge {
|
||||
if display == "wayland:native" { return start_wayland(); }
|
||||
if display == "wayland:portal" {
|
||||
if display == "wayland:portal" || display.starts_with("kms:") {
|
||||
let (incoming,_)=watch::channel(None);let (_,outgoing)=watch::channel(None);
|
||||
let (sender,ready)=tokio::sync::oneshot::channel();let _=sender.send(false);
|
||||
return ClipboardBridge {incoming,outgoing,ready};
|
||||
|
||||
+21
-16
@@ -4,9 +4,13 @@ pub fn run() -> Result<(),String> {
|
||||
if std::fs::read_to_string("/proc/sys/kernel/hostname").unwrap_or_default().trim()!="uu-hyprland-test" {
|
||||
return Err("Clipboard acceptance is restricted to the isolated VM".into());
|
||||
}
|
||||
let session=Arc::new(crate::portal::PortalSession::open()?);
|
||||
let bridge=crate::clipboard::start_portal(session.clone());
|
||||
if !bridge.ready.blocking_recv().unwrap_or(false) { return Err("Portal clipboard permission not granted".into()); }
|
||||
let screen=crate::capture::ScreenCapture::new()?;
|
||||
let input=Arc::new(crate::input::InputInjector::new());
|
||||
input.bind_portal(screen.portal.clone());
|
||||
let pointer=input.start_pointer_worker();
|
||||
let bridge=if let Some(session)=screen.portal.clone() { crate::clipboard::start_portal(session) }
|
||||
else { crate::clipboard::start(screen.display.clone()) };
|
||||
if !bridge.ready.blocking_recv().unwrap_or(false) { return Err("Desktop clipboard permission not granted".into()); }
|
||||
let mut child=Command::new("python3").arg("/home/uuremote/clipboard-acceptance.py")
|
||||
.env("GDK_BACKEND","wayland").env("GSK_RENDERER","cairo")
|
||||
.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::null()).spawn().map_err(|_|"Start GTK clipboard fixture")?;
|
||||
@@ -29,13 +33,14 @@ pub fn run() -> Result<(),String> {
|
||||
bridge.incoming.send_replace(Some(text.clone()));
|
||||
let deadline=Instant::now()+Duration::from_secs(6);let mut matched=false;
|
||||
while Instant::now()<deadline { if check("read",&text)? {matched=true;break;} std::thread::sleep(Duration::from_millis(100)); }
|
||||
if !matched { return Err(format!("Portal clipboard incoming {name} mismatch")); }
|
||||
if !matched { return Err(format!("Desktop clipboard incoming {name} mismatch")); }
|
||||
std::thread::sleep(Duration::from_millis(300));
|
||||
if outgoing.has_changed().unwrap_or(false) { return Err("Portal clipboard echoed its own selection".into()); }
|
||||
println!("Portal clipboard incoming {name}: passed; no echo");
|
||||
if outgoing.has_changed().unwrap_or(false) { return Err("Desktop clipboard echoed its own selection".into()); }
|
||||
println!("Desktop clipboard incoming {name}: passed; no echo");
|
||||
}
|
||||
session.move_pointer(f64::from(session.width)/2.0,f64::from(session.height)/2.0,false)?;
|
||||
session.button(1,true)?; session.button(1,false)?;
|
||||
input.mouse_absolute_normalized(0.5,0.5,(screen.width,screen.height));
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
input.mouse_button(1,true); input.mouse_button(1,false);
|
||||
std::thread::sleep(Duration::from_millis(300));
|
||||
let external="UU outgoing 中文🚀\nsecond line";
|
||||
if !check("set",external)? { return Err("GTK clipboard ownership failed".into()); }
|
||||
@@ -44,25 +49,25 @@ pub fn run() -> Result<(),String> {
|
||||
if outgoing.has_changed().unwrap_or(false) && outgoing.borrow_and_update().as_deref()==Some(external) {matched=true;break;}
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
if !matched { return Err("Portal clipboard outgoing Unicode mismatch".into()); }
|
||||
println!("Portal clipboard outgoing unicode: passed");
|
||||
if !matched { return Err("Desktop clipboard outgoing Unicode mismatch".into()); }
|
||||
println!("Desktop clipboard outgoing unicode: passed");
|
||||
bridge.incoming.send_replace(Some("x".repeat(crate::clipboard::MAX_TEXT+1)));
|
||||
std::thread::sleep(Duration::from_millis(400));
|
||||
if !check("read",external)? { return Err("Oversized text altered native clipboard".into()); }
|
||||
println!("Portal clipboard incoming oversized rejection: passed");
|
||||
println!("Desktop clipboard incoming oversized rejection: passed");
|
||||
if !check("set",&"y".repeat(crate::clipboard::MAX_TEXT+1))? { return Err("GTK oversized fixture setup failed".into()); }
|
||||
std::thread::sleep(Duration::from_secs(3));
|
||||
if outgoing.has_changed().unwrap_or(false) { return Err("Portal clipboard published oversized native text".into()); }
|
||||
println!("Portal clipboard outgoing oversized rejection: passed");
|
||||
if outgoing.has_changed().unwrap_or(false) { return Err("Desktop clipboard published oversized native text".into()); }
|
||||
println!("Desktop clipboard outgoing oversized rejection: passed");
|
||||
if !check("set","")? { return Err("GTK empty fixture setup failed".into()); }
|
||||
let deadline=Instant::now()+Duration::from_secs(6);let mut matched=false;
|
||||
while Instant::now()<deadline {
|
||||
if outgoing.has_changed().unwrap_or(false) && outgoing.borrow_and_update().as_deref()==Some("") {matched=true;break;}
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
if !matched { return Err("Portal clipboard outgoing empty text mismatch".into()); }
|
||||
println!("Portal clipboard outgoing empty: passed");
|
||||
if !matched { return Err("Desktop clipboard outgoing empty text mismatch".into()); }
|
||||
println!("Desktop clipboard outgoing empty: passed");
|
||||
drop(writer); Ok(())
|
||||
})();
|
||||
let _=child.kill();let _=child.wait();result
|
||||
let _=child.kill();let _=child.wait();pointer.abort();input.release_held();result
|
||||
}
|
||||
@@ -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.
|
||||
#[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> {
|
||||
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 {
|
||||
Some("x11") => Ok(DesktopBackend::X11),
|
||||
Some("wayland") => Ok(DesktopBackend::Wayland),
|
||||
Some("kms") => Ok(DesktopBackend::Kms),
|
||||
Some("portal") => Ok(DesktopBackend::Portal),
|
||||
Some("none") => Ok(DesktopBackend::X11), // never opened, see headless()
|
||||
None | Some("auto") => {
|
||||
if session == Some("wayland") || socket.is_some_and(|s| !s.is_empty()) {
|
||||
Ok(DesktopBackend::Wayland)
|
||||
} 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,Some("x11"),None),Ok(DesktopBackend::X11));
|
||||
assert_eq!(select(Some("x11"),Some("wayland"),Some("wayland-0")),Ok(DesktopBackend::X11));
|
||||
assert_eq!(select(Some("kms"),Some("wayland"),Some("wayland-0")),Ok(DesktopBackend::Kms));
|
||||
assert!(select(Some("typo"),None,None).is_err());
|
||||
}
|
||||
}
|
||||
@@ -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(())
|
||||
}
|
||||
+252
-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_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_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"))]
|
||||
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"))]
|
||||
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)]
|
||||
struct PendingPointer {
|
||||
absolute: Option<(i32,i32)>,
|
||||
@@ -33,11 +40,22 @@ struct PendingPointer {
|
||||
unsafe fn x11_pointer_state(_: *mut libc::c_int,_:*mut libc::c_int,_:*mut libc::c_uint)->libc::c_int {-1}
|
||||
#[derive(Default)]
|
||||
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> }
|
||||
|
||||
pub struct InputInjector { 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 {
|
||||
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 { 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>>) {
|
||||
@@ -45,10 +63,12 @@ impl InputInjector {
|
||||
*self.portal.lock().unwrap()=session;
|
||||
}
|
||||
pub fn mouse_move(&self, x: i32, y: i32) {
|
||||
let _serial=self.serial.lock().unwrap();
|
||||
self.inject_move(x,y,false);
|
||||
}
|
||||
|
||||
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) {
|
||||
let mut pending = self.pending.lock().unwrap();
|
||||
pending.absolute = Some(position);
|
||||
@@ -78,6 +98,10 @@ impl InputInjector {
|
||||
}
|
||||
pub fn discard_pointer(&self) { *self.pending.lock().unwrap() = PendingPointer::default(); }
|
||||
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
|
||||
// overwrite a newer position flushed before a button transition.
|
||||
let mut pending = self.pending.lock().unwrap();
|
||||
@@ -103,7 +127,8 @@ impl InputInjector {
|
||||
}
|
||||
}
|
||||
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);
|
||||
if result == 0 {
|
||||
let mut held = self.held.lock().unwrap();
|
||||
@@ -117,7 +142,48 @@ 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
|
||||
}
|
||||
|
||||
pub fn windows_key_event(&self, key: i32, is_press: bool) {
|
||||
let _serial=self.serial.lock().unwrap();
|
||||
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) {
|
||||
let result = self.inject_key(symbol,is_press);
|
||||
static AUDIT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||
@@ -132,11 +198,29 @@ impl InputInjector {
|
||||
}
|
||||
|
||||
pub fn release_held(&self) {
|
||||
let _serial=self.serial.lock().unwrap();
|
||||
self.discard_pointer();
|
||||
let mut held=self.held.lock().unwrap();
|
||||
for button in &held.buttons {self.inject_button(*button,false);}
|
||||
for key in &held.keys {self.inject_key(*key,false);}
|
||||
if let Some(uinput)=&self.uinput {
|
||||
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();
|
||||
*self.shortcuts.lock().unwrap()=Shortcuts::default();
|
||||
}
|
||||
// Per-injector budgets cover missing bindings, locked sessions, stale handles
|
||||
// and D-Bus errors. Never include keys, coordinates, text or portal tokens.
|
||||
@@ -158,25 +242,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 {
|
||||
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 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) }
|
||||
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 {
|
||||
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 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) }
|
||||
else { unsafe { x11_mouse_button(button,i32::from(pressed)) } }
|
||||
}
|
||||
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 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) }
|
||||
else { unsafe { x11_key_symbol(symbol as libc::c_ulong,i32::from(pressed)) } }
|
||||
}
|
||||
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; }
|
||||
unsafe { x11_key_event(keycode, if is_press { 1 } else { 0 }) };
|
||||
}
|
||||
@@ -213,6 +326,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)]
|
||||
mod scroll_tests {
|
||||
use super::*;
|
||||
@@ -227,3 +363,114 @@ mod scroll_tests {
|
||||
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()) }
|
||||
}
|
||||
+77
-4
@@ -12,17 +12,24 @@ mod login;
|
||||
mod login_ui;
|
||||
mod session_lock;
|
||||
mod capture;
|
||||
mod kms;
|
||||
mod file_trans;
|
||||
mod input;
|
||||
mod uinput;
|
||||
mod input_mapping;
|
||||
mod port_forward;
|
||||
mod port_mapping;
|
||||
mod power;
|
||||
mod signaling;
|
||||
mod terminal;
|
||||
mod webrtc;
|
||||
mod video;
|
||||
#[cfg(feature = "inproc-av")]
|
||||
mod damage_capture;
|
||||
#[cfg(feature = "inproc-av")]
|
||||
mod inproc_encoder;
|
||||
mod capture_setting;
|
||||
mod audio;
|
||||
mod virtual_audio;
|
||||
mod system_actions;
|
||||
mod capability;
|
||||
mod protocol;
|
||||
@@ -50,7 +57,7 @@ use webrtc::WebRtcSession;
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
|
||||
// An unknown argument must never fall through to starting a second full host server.
|
||||
const SUBCOMMANDS: [&str; 8] = ["login", "unlock-check", "desktop-probe", "desktop-video-probe", "wayland-capture",
|
||||
const SUBCOMMANDS: [&str; 11] = ["desktop-input-probe", "virtual-audio-probe", "login", "unlock-check", "desktop-probe", "desktop-video-probe", "desktop-audio-probe", "wayland-capture",
|
||||
"wayland-input-probe", "clipboard-acceptance", "portal-acceptance"];
|
||||
if let Some(arg) = std::env::args().nth(1).filter(|a| !SUBCOMMANDS.contains(&a.as_str())) {
|
||||
let usage = format!("usage: uuremote-server [{}]\n (no argument: run the host)", SUBCOMMANDS.join(" | "));
|
||||
@@ -159,6 +166,48 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let path=std::env::args().nth(2);
|
||||
return video::probe((screen.width,screen.height),screen.display.clone(),screen.portal.clone(),path).await.map_err(|e|e.into());
|
||||
}
|
||||
if std::env::args().nth(1).as_deref() == Some("desktop-input-probe") {
|
||||
if std::fs::read_to_string("/proc/sys/kernel/hostname").unwrap_or_default().trim()!="uu-hyprland-test" {
|
||||
return Err("Input acceptance is restricted to the isolated VM".into());
|
||||
}
|
||||
let screen=ScreenCapture::new()?;
|
||||
let input=Arc::new(InputInjector::new());
|
||||
input.bind_portal(screen.portal.clone());
|
||||
let worker=input.start_pointer_worker();
|
||||
// libinput discovers newly created uinput devices asynchronously.
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
input.mouse_absolute_normalized(0.25,0.25,(screen.width,screen.height));
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
input.mouse_absolute_normalized(0.5,0.5,(screen.width,screen.height));
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
input.mouse_button(1,true); input.mouse_button(1,false);
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
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") {
|
||||
return wayland::input_probe().map_err(|e|e.into());
|
||||
}
|
||||
@@ -185,9 +234,22 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
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());
|
||||
}
|
||||
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") {
|
||||
return session_lock::check_from_stdin().map_err(|e| e.into());
|
||||
}
|
||||
if std::env::args().nth(1).as_deref() == Some("virtual-audio-probe") {
|
||||
virtual_audio::init();
|
||||
if !virtual_audio::enable() { return Err("Virtual audio probe setup failed".into()); }
|
||||
println!("Virtual audio probe active");
|
||||
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
|
||||
virtual_audio::disable();
|
||||
if virtual_audio::active() { return Err("Virtual audio probe restore incomplete".into()); }
|
||||
println!("Virtual audio probe ended");
|
||||
return Ok(());
|
||||
}
|
||||
if std::env::args().nth(1).as_deref() == Some("login") {
|
||||
// Interactive: register this host and bind it to an account via QR scan
|
||||
// (GTK window when available; `login --no-gui` keeps it terminal-only).
|
||||
@@ -195,9 +257,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
if login::run(gui).await.is_err() { std::process::exit(1); } // error already shown
|
||||
return Ok(());
|
||||
}
|
||||
virtual_audio::init();
|
||||
let _audio_cleanup=virtual_audio::Cleanup;
|
||||
log::info!("Starting uu-remote-linux host v{}...", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
let mut capture = Arc::new((if desktop_backend::detect()?==desktop_backend::DesktopBackend::Portal
|
||||
let mut 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") {
|
||||
Err("Portal screen access requires a desktop connection and local consent".to_owned())
|
||||
} else { ScreenCapture::new() }).unwrap_or_else(|error| {
|
||||
@@ -205,13 +271,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
ScreenCapture::unavailable(error)
|
||||
}));
|
||||
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();
|
||||
#[cfg(target_os="linux")]
|
||||
{
|
||||
let shutdown_input=input.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Ok(mut term)=tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
|
||||
term.recv().await; shutdown_input.release_held(); terminal::shutdown_all(); std::process::exit(0);
|
||||
tokio::select! { _=term.recv()=>{}, _=tokio::signal::ctrl_c()=>{} }
|
||||
shutdown_input.shutdown(); virtual_audio::disable(); terminal::shutdown_all(); std::process::exit(0);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -351,6 +420,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
let stale_portal=capture.portal.as_ref().is_some_and(|session|!session.is_alive())
|
||||
&& 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) {
|
||||
if stale_portal {
|
||||
input.release_held();input.bind_portal(None);
|
||||
|
||||
@@ -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 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> {
|
||||
let mut handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?;
|
||||
let locked = *self.locked.borrow() != Some(false);
|
||||
if locked || self.was_locked.swap(locked, std::sync::atomic::Ordering::AcqRel) {
|
||||
self.was_locked.store(locked, std::sync::atomic::Ordering::Release);
|
||||
let state = *self.locked.borrow();
|
||||
if state != Some(false) {
|
||||
// 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()) };
|
||||
super::blank(frame); return Ok(());
|
||||
}
|
||||
|
||||
+122
-8
@@ -47,6 +47,51 @@ pub enum SignalingEvent {
|
||||
|
||||
pub struct SignalingClient {
|
||||
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 {
|
||||
@@ -79,7 +124,7 @@ impl SignalingClient {
|
||||
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 {
|
||||
if let Err(e) = write.send(Message::Text(msg)).await {
|
||||
log::warn!("WS send error: {}", e);
|
||||
@@ -89,23 +134,66 @@ impl SignalingClient {
|
||||
});
|
||||
|
||||
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 {
|
||||
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).
|
||||
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 {
|
||||
Ok(Message::Text(txt)) => {
|
||||
// Engine.IO 报文处理
|
||||
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
|
||||
let _ = tx_out_clone.send("40".into()).await;
|
||||
} else if txt == "2" {
|
||||
// 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") {
|
||||
connected=true;
|
||||
next_refresh=tokio::time::Instant::now()+std::time::Duration::from_secs(30);
|
||||
// Socket.IO connected -> 发送 room_info 请求
|
||||
req_counter += 1;
|
||||
let room_req = format!("42{}[\"room_info\"]", req_counter);
|
||||
@@ -113,9 +201,23 @@ impl SignalingClient {
|
||||
let _ = event_sender.send(SignalingEvent::Connected).await;
|
||||
} else if txt.starts_with("43") {
|
||||
// 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) {
|
||||
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") {
|
||||
// Socket.IO binary event/ack: header text, attachments follow as binary frames.
|
||||
@@ -170,14 +272,16 @@ impl SignalingClient {
|
||||
}
|
||||
}
|
||||
"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
|
||||
.send(SignalingEvent::Released(data.clone()))
|
||||
.await;
|
||||
}
|
||||
"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("");
|
||||
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;
|
||||
} else {
|
||||
let _ = event_sender
|
||||
@@ -212,16 +316,26 @@ impl SignalingClient {
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
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> {
|
||||
|
||||
+10
-5
@@ -113,10 +113,14 @@ pub fn args_shape(args: &[u8]) -> String {
|
||||
}
|
||||
|
||||
fn mute_for_session(state: &mut HostState) {
|
||||
if state.restore.is_none() { state.restore = host_muted(); }
|
||||
set_host_muted(true);
|
||||
if state.restore.is_none() {
|
||||
state.restore = host_muted();
|
||||
set_host_muted(true);
|
||||
}
|
||||
crate::virtual_audio::enable();
|
||||
}
|
||||
fn restore_mute(state: &mut HostState) {
|
||||
crate::virtual_audio::disable();
|
||||
if let Some(previous) = state.restore.take() { set_host_muted(previous); }
|
||||
}
|
||||
|
||||
@@ -143,20 +147,20 @@ impl PeerActions {
|
||||
let on = toggle_value(value, args);
|
||||
state.settings.keep_mute = on;
|
||||
if on { mute_for_session(&mut state); } else { restore_mute(&mut state); }
|
||||
log::info!("System action mute (action {}): on={} muted={:?}", action, on, host_muted());
|
||||
log::info!("System action mute (action {}): on={} virtual_sink={} default_muted={:?}", action, on, crate::virtual_audio::active(), host_muted());
|
||||
}
|
||||
// Keep the host muted after the session (远程结束后,恢复被控端声音设置 unchecked).
|
||||
MUTE_PERMANENT => {
|
||||
let on = toggle_value(value, args);
|
||||
state.settings.mute_permanent = on;
|
||||
if on { state.settings.keep_mute = true; mute_for_session(&mut state); }
|
||||
log::info!("System action mute_permanent: on={} muted={:?}", on, host_muted());
|
||||
log::info!("System action mute_permanent: on={} virtual_sink={} default_muted={:?}", on, crate::virtual_audio::active(), host_muted());
|
||||
}
|
||||
MUTE_RESTORE => {
|
||||
state.settings.keep_mute = false;
|
||||
state.settings.mute_permanent = false;
|
||||
restore_mute(&mut state);
|
||||
log::info!("System action mute_restore: muted={:?}", host_muted());
|
||||
log::info!("System action mute_restore: virtual_sink={} default_muted={:?}", crate::virtual_audio::active(), host_muted());
|
||||
}
|
||||
SET_AUTO_LOCK => {
|
||||
// Observed: enabling 远程结束后自动锁屏 sends value 2 and disabling value 1.
|
||||
@@ -188,6 +192,7 @@ impl PeerActions {
|
||||
let mut state = state().lock().unwrap();
|
||||
state.desktop_sessions = state.desktop_sessions.saturating_sub(1);
|
||||
if state.desktop_sessions > 0 { return; }
|
||||
crate::virtual_audio::disable();
|
||||
if !state.settings.mute_permanent { restore_mute(&mut state); }
|
||||
if state.settings.auto_lock {
|
||||
log::info!("Auto lock after remote session: ok={}", lock_session());
|
||||
|
||||
+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 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;
|
||||
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}"))?;
|
||||
@@ -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}"))?;
|
||||
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,
|
||||
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}"))?);
|
||||
@@ -207,12 +213,14 @@ impl TerminalConn {
|
||||
let (cols, rows) = dims(&v);
|
||||
let session = match existing {
|
||||
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 {
|
||||
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"));
|
||||
*sink = Some(self.out.clone());
|
||||
let replay: Vec<u8> = s.scrollback.lock().unwrap().iter().copied().collect();
|
||||
if !replay.is_empty() { self.send(STDOUT, s.id, &replay); }
|
||||
}
|
||||
@@ -241,7 +249,8 @@ impl TerminalConn {
|
||||
"state": if attached { "running" } else { "detached" }, "created_at_ms": s.created_ms})
|
||||
}).collect();
|
||||
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")),
|
||||
_ => self.send(ERROR, frame.session, &reply(1, "unsupported frame")),
|
||||
@@ -268,25 +277,51 @@ mod tests {
|
||||
assert_eq!(signal_number(b"whatever"), libc::SIGINT);
|
||||
}
|
||||
#[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 conn = TerminalConn::new(tx);
|
||||
conn.handle(&encode(OPEN, 0, 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);
|
||||
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();
|
||||
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)); }
|
||||
for request in [0, 7, u32::MAX] {
|
||||
conn.handle(&encode(LIST_REQ, request, &[]));
|
||||
let response = decode(&rx.try_recv().unwrap()).unwrap();
|
||||
assert_eq!(response.kind, LIST_RESP);
|
||||
assert_eq!(response.session, request);
|
||||
assert!(json(&response.payload).is_array());
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn open_spawns_shell_and_streams_output() {
|
||||
for requested_id in [0, u32::MAX] {
|
||||
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
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::atomic::AtomicBool;
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::process::Command;
|
||||
@@ -11,10 +12,20 @@ use crate::capture_setting::VideoSettings;
|
||||
|
||||
/// Default frame rate; the viewer's 帧率 setting overrides it per connection.
|
||||
pub const FPS: u64 = 60;
|
||||
/// IDR every 0.5s. All-intra 60fps measured 30 Mbit/s on the real desktop (saturating
|
||||
/// the viewer's Wi-Fi: 3-6 fps received); GOP 30 measured 2.7 Mbit/s at the same CQ18.
|
||||
/// A broken prediction chain therefore freezes at most this long.
|
||||
const GOP: u64 = 30;
|
||||
/// Keyframe interval. All-intra 60fps measured 30 Mbit/s on the real desktop (saturating the
|
||||
/// viewer's Wi-Fi: 3-6 fps received). On a static desktop with NVENC CQ18 a 0.5s GOP measured
|
||||
/// 7.7 Mbit/s and a 2s GOP 1.9 Mbit/s. A broken prediction chain freezes until the next IDR or
|
||||
/// 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;
|
||||
|
||||
fn nal_offsets(bytes: &[u8], kind: u8) -> Vec<usize> {
|
||||
@@ -42,9 +53,36 @@ struct CompleteFrame {
|
||||
|
||||
const RTP_CLOCK: u64 = 90_000;
|
||||
|
||||
/// `UU_VIDEO_ENCODER=cpu` keeps the virtual CPU display entirely off the GPU.
|
||||
pub fn gpu_encoder_requested() -> bool {
|
||||
std::env::var("UU_VIDEO_ENCODER").map_or(true, |v| v != "cpu")
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(crate) enum Encoder { Cpu, Nvenc, Vaapi }
|
||||
impl Encoder {
|
||||
pub(crate) fn requested() -> Self { Self::from_setting(std::env::var("UU_VIDEO_ENCODER").ok().as_deref(), 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`.
|
||||
@@ -89,53 +127,119 @@ impl DependencyGate{
|
||||
// Aborting the owner task drops Child, whose kill_on_drop kills only this encoder.
|
||||
struct EncoderTask(tokio::task::JoinHandle<Result<(), String>>);
|
||||
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(
|
||||
size: (u32, u32), display: String, portal:Option<Arc<crate::portal::PortalSession>>,
|
||||
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> {
|
||||
// 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; }
|
||||
match read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),true,sequence.clone(),settings).await {
|
||||
let encoder = Encoder::requested();
|
||||
if encoder == Encoder::Cpu { return read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr).await; }
|
||||
match read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),encoder,sequence.clone(),settings,idr.clone()).await {
|
||||
Ok(())=>Ok(()),
|
||||
Err(error) if !latest.is_closed()=>{
|
||||
log::warn!("NVENC stream unavailable; switching scoped encoder to CPU: {}",error);
|
||||
read_encoder_with(size,display,portal,latest,false,sequence,settings).await
|
||||
log::warn!("{} stream unavailable; switching scoped encoder to CPU: {}",encoder.name(),error);
|
||||
read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr).await
|
||||
},
|
||||
Err(error)=>Err(error),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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
|
||||
/// 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 buffer_kbits = (max_kbps / 8).clamp(100, 500);
|
||||
(format!("{max_kbps}k"), format!("{buffer_kbits}k"))
|
||||
(format!("{max_kbps}k"), format!("{}k", buffer_kbits(max_kbps, encoder)))
|
||||
}
|
||||
|
||||
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>,
|
||||
mut settings: VideoSettings,
|
||||
size: (u32,u32),display:String,portal:Option<Arc<crate::portal::PortalSession>>,latest:mpsc::Sender<Arc<CompleteFrame>>,encoder:Encoder,sequence:Arc<std::sync::atomic::AtomicU64>,
|
||||
settings: VideoSettings, #[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] idr: Arc<AtomicBool>,
|
||||
)->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 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();
|
||||
// Keep the IDR interval at 0.5 s whatever the frame rate.
|
||||
let gop = (u64::from(settings.fps) * GOP / FPS).max(1).to_string();
|
||||
let (maxrate, bufsize) = rate_args(&settings);
|
||||
// Keep the IDR interval at 2 s whatever the frame rate.
|
||||
let gop = gop_frames(settings.fps, false).to_string();
|
||||
let (maxrate, bufsize) = rate_args(&settings, encoder);
|
||||
let cq = settings.cq().to_string();
|
||||
let target = settings.max_kbps().map_or("16M".to_string(), |k| format!("{}k", k * 3 / 4));
|
||||
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,if gpu{"h264_nvenc"}else{"libx264"});
|
||||
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,encoder.name());
|
||||
// A bundled FFmpeg prefix (config ffmpeg_root / UU_FFMPEG_ROOT) or `ffmpeg` on PATH.
|
||||
let mut command=match crate::config::ffmpeg_root() {
|
||||
Some(root) => { let mut c=Command::new(root.join("bin/ffmpeg")); c.env("LD_LIBRARY_PATH",root.join("lib/x86_64-linux-gnu")); c }
|
||||
None => Command::new("ffmpeg"),
|
||||
};
|
||||
command.args(["-hide_banner","-loglevel","error","-nostdin"]);
|
||||
if encoder == Encoder::Vaapi { command.args(["-vaapi_device", &vaapi_device()]); }
|
||||
if wayland {
|
||||
command.args(["-f","rawvideo","-pixel_format","bgra","-video_size",&size,
|
||||
"-framerate",&fps,"-i","pipe:0","-an"]);
|
||||
@@ -145,10 +249,14 @@ async fn read_encoder_with(
|
||||
"-video_size",&size,"-i",&display,"-an"]);
|
||||
}
|
||||
// 其他分辨率: scale the captured desktop (the host display mode is left untouched).
|
||||
if let Some((width, height)) = settings.output {
|
||||
if encoder == Encoder::Vaapi {
|
||||
command.args(["-vf", &vaapi_filter(settings.output)]);
|
||||
} else if let Some((width, height)) = settings.output {
|
||||
command.args(["-vf", &format!("scale={width}:{height}:flags=bilinear")]);
|
||||
}
|
||||
if gpu{
|
||||
if encoder == Encoder::Vaapi {
|
||||
command.args(vaapi_options(&settings, &gop));
|
||||
} else if encoder == Encoder::Nvenc {
|
||||
command.args(["-c:v","h264_nvenc","-preset","p4","-tune","ull",
|
||||
"-profile:v","baseline","-pix_fmt","yuv420p","-g",&gop,"-bf","0",
|
||||
"-rc","vbr","-cq",&cq,"-b:v",&target,"-maxrate",&maxrate,"-bufsize",&bufsize,
|
||||
@@ -172,7 +280,23 @@ async fn read_encoder_with(
|
||||
.stderr(std::process::Stdio::inherit())
|
||||
.spawn().map_err(|e| format!("Start scoped encoder: {e}"))?;
|
||||
// Scope capture to this encoder. Dropping either task kills its child.
|
||||
let _capture=if let Some(session)=portal {
|
||||
let _capture=if let Some(capture)=kms_capture {
|
||||
let mut sink=child.stdin.take().ok_or("Encoder stdin missing")?;
|
||||
Some(EncoderTask(tokio::spawn(async move {
|
||||
use tokio::io::AsyncWriteExt;
|
||||
let mut tick=tokio::time::interval(Duration::from_nanos(1_000_000_000/u64::from(settings.fps)));
|
||||
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
loop {
|
||||
tick.tick().await;
|
||||
let capture=capture.clone();
|
||||
let bytes=tokio::task::spawn_blocking(move || {
|
||||
let mut bytes=vec![0;capture.width as usize*capture.height as usize*4];
|
||||
capture.capture(&mut bytes)?; Ok::<_,String>(bytes)
|
||||
}).await.map_err(|_|"KMS worker failed")??;
|
||||
sink.write_all(&bytes).await.map_err(|_|"KMS encoder pipe failed")?;
|
||||
}
|
||||
})))
|
||||
} else if let Some(session)=portal {
|
||||
let mut sink=child.stdin.take().ok_or("Encoder stdin missing")?;
|
||||
Some(EncoderTask(tokio::spawn(async move {
|
||||
use tokio::io::AsyncWriteExt;
|
||||
@@ -197,7 +321,7 @@ async fn read_encoder_with(
|
||||
let executable=std::path::PathBuf::from("/proc/self/exe");
|
||||
#[cfg(not(target_os="linux"))]
|
||||
let executable=std::env::current_exe().map_err(|e|format!("Capture executable: {e}"))?;
|
||||
let mut capture=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())
|
||||
.spawn().map_err(|e|format!("Start Wayland capture: {e}"))?;
|
||||
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 {
|
||||
matches!(pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed)
|
||||
|| 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 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 bytes=0;
|
||||
tokio::time::timeout(Duration::from_secs(15),async {
|
||||
while count<5 {
|
||||
// UU_PROBE_FRAMES lengthens the sample for baseline measurements; default is 5 frames.
|
||||
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")?;
|
||||
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())?; }
|
||||
@@ -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>>,
|
||||
mut settings: tokio::sync::watch::Receiver<VideoSettings>) -> Result<(), String> {
|
||||
mut settings: tokio::sync::watch::Receiver<VideoSettings>, idr: Arc<AtomicBool>) -> Result<(), String> {
|
||||
loop {
|
||||
if transport_closed(&pc) { return Ok(()); }
|
||||
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 sequence = Arc::new(std::sync::atomic::AtomicU64::new(0));
|
||||
let current = *settings.borrow_and_update();
|
||||
let mut reader = EncoderTask(tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone())));
|
||||
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 timeline = RtpTimeline { fps: u64::from(current.fps), ..Default::default() };
|
||||
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.
|
||||
let next = *settings.borrow_and_update();
|
||||
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() => {
|
||||
// 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)]
|
||||
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::*;
|
||||
#[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() {
|
||||
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]);
|
||||
|
||||
@@ -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_key(symbol: u64, pressed: i32) -> 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> {
|
||||
#[cfg(all(target_os="linux", feature="wayland"))]
|
||||
@@ -58,7 +65,8 @@ pub fn capture_stdout() -> Result<(),String> {
|
||||
let (w,h)=init()?;
|
||||
let mut frame=vec![0u8;(w as usize)*(h as usize)*4];
|
||||
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 {
|
||||
let start=std::time::Instant::now();
|
||||
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.
|
||||
pub fn input_probe()->Result<(),String> {
|
||||
let (w,h)=init()?;
|
||||
|
||||
+84
-7
@@ -86,6 +86,7 @@ impl WebRtcSession {
|
||||
.map_err(|e| format!("Create PeerConnection failed: {}", e))?,
|
||||
);
|
||||
|
||||
let input_session=desktop.then(||input.desktop_session());
|
||||
let context = Arc::new(Mutex::new(SessionContext::default()));
|
||||
crate::network_diagnostics::start(Arc::downgrade(&pc));
|
||||
|
||||
@@ -143,7 +144,7 @@ impl WebRtcSession {
|
||||
}));
|
||||
|
||||
let connected_context = context.clone();
|
||||
let connection_input=input.clone();
|
||||
let connection_input=input_session.clone();
|
||||
let connected_signal = signaling_client.clone();
|
||||
let notified = Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||
pc.on_peer_connection_state_change(Box::new(move |state| {
|
||||
@@ -153,7 +154,10 @@ impl WebRtcSession {
|
||||
let notified = notified.clone();
|
||||
Box::pin(async move {
|
||||
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) {
|
||||
tokio::spawn(async move {
|
||||
let ctx = ctx.lock().await.clone();
|
||||
@@ -184,8 +188,11 @@ impl WebRtcSession {
|
||||
"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
|
||||
.map_err(|e| format!("Add video track failed: {}", e))?;
|
||||
let idr_on_feedback = idr_request.clone();
|
||||
tokio::spawn(async move {
|
||||
let mut feedback = [0u8; 1500];
|
||||
let mut reports = 0usize;
|
||||
@@ -193,6 +200,14 @@ impl WebRtcSession {
|
||||
while let Ok((packets, _)) = sender.read(&mut feedback).await {
|
||||
for packet in packets {
|
||||
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>() {
|
||||
reports += 1;
|
||||
if reports == 1 || reports % 10 == 0 {
|
||||
@@ -261,7 +276,7 @@ impl WebRtcSession {
|
||||
// File/terminal/port-mapping connections carry no desktop video.
|
||||
if desktop {
|
||||
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);
|
||||
}
|
||||
});
|
||||
@@ -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_tx, mut file_rx) = tokio::sync::mpsc::unbounded_channel::<Vec<u8>>();
|
||||
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();
|
||||
tokio::spawn(async move {
|
||||
@@ -304,10 +331,12 @@ impl WebRtcSession {
|
||||
log::info!("New DataChannel received: {}", label);
|
||||
|
||||
let input_inst = input_clone.clone();
|
||||
let channel_input_session=input_session.clone();
|
||||
let screen = control_capture.clone();
|
||||
let out_sequence = control_sequence.clone();
|
||||
let shared_clipboard_ready = text_clipboard_ready.clone();
|
||||
let peer_files = peer_files.clone();
|
||||
let peer_port_mapping = port_mapping.clone();
|
||||
let peer_video_settings = video_settings.clone();
|
||||
let peer_actions = peer_actions.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 rich_state=Arc::new(std::sync::Mutex::new(crate::clipboard_rich::State::default()));
|
||||
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.
|
||||
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);
|
||||
// Default file-transfer folder: the desktop user's home directory.
|
||||
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
|
||||
} 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 = [
|
||||
(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)))),
|
||||
(26, Ok(crate::initialization::envelope(next_seq(), timestamp, 26,
|
||||
&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 {
|
||||
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 {
|
||||
Ok(message) => message,
|
||||
Err(e) => { log::error!("CONTROL initialization encode failed: {}", e); return; }
|
||||
@@ -470,6 +510,7 @@ impl WebRtcSession {
|
||||
d.on_message(Box::new(move |msg: DataChannelMessage| {
|
||||
let channel_name = lbl.clone();
|
||||
let input_worker = inp.clone();
|
||||
let message_input_session=channel_input_session.clone();
|
||||
let terminal_worker = terminal_conn.clone();
|
||||
let rpc_out = outbound.clone();
|
||||
let reply_chan = d_reply.clone();
|
||||
@@ -479,6 +520,7 @@ impl WebRtcSession {
|
||||
let clipboard_ready = clipboard_enabled.clone();
|
||||
let rich_incoming=rich_state.clone();
|
||||
let file_worker = file_session.clone();
|
||||
let mapping = peer_port_mapping.clone();
|
||||
let video_settings = channel_video_settings.clone();
|
||||
let actions = channel_actions.clone();
|
||||
|
||||
@@ -517,6 +559,9 @@ impl WebRtcSession {
|
||||
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 Ok(fields) = crate::protocol::decode_wire(&msg.data) {
|
||||
if files.handle(&fields) {
|
||||
@@ -728,6 +773,7 @@ impl WebRtcSession {
|
||||
if RECEIPTS[slot].fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 16 {
|
||||
log::info!("CONTROL input received: kind={}", kind);
|
||||
}
|
||||
if let Some(session)=message_input_session.as_ref() {session.with_input(|| {
|
||||
match &event {
|
||||
ControlInput::MouseAbsolute {x,y} => input_worker.mouse_absolute_normalized(*x,*y,input_size),
|
||||
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);},
|
||||
_ => {}
|
||||
}
|
||||
});}
|
||||
|
||||
},
|
||||
_ => {}
|
||||
@@ -841,7 +888,11 @@ impl WebRtcSession {
|
||||
.set_local_description(answer.clone())
|
||||
.await
|
||||
.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
|
||||
@@ -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)> {
|
||||
let preferred: Vec<&str> = sdp.lines().find_map(|l| l.strip_prefix("a=group:BUNDLE "))?
|
||||
.split_whitespace().collect();
|
||||
@@ -907,6 +975,15 @@ fn negotiated_bundle_media(sdp: &str) -> Option<(String, u16)> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
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]
|
||||
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";
|
||||
|
||||
+15
-1
@@ -6,6 +6,17 @@ final class MarkerCheck: NSObject {
|
||||
let kde = NSTextField(labelWithString: "KDE → Mac: not checked")
|
||||
let copied = NSTextField(labelWithString: "Mac marker: not copied")
|
||||
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() {
|
||||
NSPasteboard.general.clearContents()
|
||||
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)
|
||||
}
|
||||
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.isReleasedWhenClosed = false
|
||||
let stack = NSStackView(views: [
|
||||
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("Compare GNOME VM marker", #selector(compareGnome)), gnome,
|
||||
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))])
|
||||
stack.orientation = .vertical; stack.spacing = 14
|
||||
stack.translatesAutoresizingMaskIntoConstraints = false
|
||||
|
||||
Vendored
+7
-3
@@ -1,5 +1,5 @@
|
||||
"""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 subprocess.check_output(['systemd-detect-virt'],text=True).strip()=='qemu'
|
||||
import gi
|
||||
@@ -7,7 +7,11 @@ gi.require_version('Gio','2.0')
|
||||
from gi.repository import Gio,GLib
|
||||
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)
|
||||
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]
|
||||
stop=threading.Event()
|
||||
signal.signal(signal.SIGTERM,lambda *_:stop.set())
|
||||
@@ -16,6 +20,6 @@ print('VM timed idle inhibition acquired',flush=True)
|
||||
try:stop.wait(seconds)
|
||||
finally:
|
||||
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)
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
use std::fmt;
|
||||
|
||||
/// ICERole describes the role ice.Agent is playing in selecting the
|
||||
/// preferred the candidate pair.
|
||||
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub enum RTCIceRole {
|
||||
#[default]
|
||||
Unspecified,
|
||||
|
||||
/// ICERoleControlling indicates that the ICE agent that is responsible
|
||||
/// for selecting the final choice of candidate pairs and signaling them
|
||||
/// through STUN and an updated offer, if needed. In any session, one agent
|
||||
/// is always controlling. The other is the controlled agent.
|
||||
Controlling,
|
||||
|
||||
/// ICERoleControlled indicates that an ICE agent that waits for the
|
||||
/// controlling agent to select the final choice of candidate pairs.
|
||||
Controlled,
|
||||
}
|
||||
|
||||
const ICE_ROLE_CONTROLLING_STR: &str = "controlling";
|
||||
const ICE_ROLE_CONTROLLED_STR: &str = "controlled";
|
||||
|
||||
impl From<&str> for RTCIceRole {
|
||||
fn from(raw: &str) -> Self {
|
||||
match raw {
|
||||
ICE_ROLE_CONTROLLING_STR => RTCIceRole::Controlling,
|
||||
ICE_ROLE_CONTROLLED_STR => RTCIceRole::Controlled,
|
||||
_ => RTCIceRole::Unspecified,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for RTCIceRole {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {
|
||||
RTCIceRole::Controlling => write!(f, "{ICE_ROLE_CONTROLLING_STR}"),
|
||||
RTCIceRole::Controlled => write!(f, "{ICE_ROLE_CONTROLLED_STR}"),
|
||||
_ => write!(f, "{}", crate::UNSPECIFIED_STR),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_new_ice_role() {
|
||||
let tests = vec![
|
||||
("Unspecified", RTCIceRole::Unspecified),
|
||||
("controlling", RTCIceRole::Controlling),
|
||||
("controlled", RTCIceRole::Controlled),
|
||||
];
|
||||
|
||||
for (role_string, expected_role) in tests {
|
||||
assert_eq!(RTCIceRole::from(role_string), expected_role);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ice_role_string() {
|
||||
let tests = vec![
|
||||
(RTCIceRole::Unspecified, "Unspecified"),
|
||||
(RTCIceRole::Controlling, "controlling"),
|
||||
(RTCIceRole::Controlled, "controlled"),
|
||||
];
|
||||
|
||||
for (proto, expected_string) in tests {
|
||||
assert_eq!(proto.to_string(), expected_string);
|
||||
}
|
||||
}
|
||||
}
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::ice_transport::ice_credential_type::RTCIceCredentialType;
|
||||
|
||||
/// ICEServer describes a single STUN and TURN server that can be used by
|
||||
/// the ICEAgent to establish a connection with a peer.
|
||||
#[derive(Default, Debug, Clone, Serialize, Deserialize, Hash)]
|
||||
pub struct RTCIceServer {
|
||||
pub urls: Vec<String>,
|
||||
pub username: String,
|
||||
pub credential: String,
|
||||
pub credential_type: RTCIceCredentialType,
|
||||
}
|
||||
|
||||
impl RTCIceServer {
|
||||
pub(crate) fn parse_url(&self, url_str: &str) -> Result<ice::url::Url> {
|
||||
Ok(ice::url::Url::parse_url(url_str)?)
|
||||
}
|
||||
|
||||
pub(crate) fn validate(&self) -> Result<()> {
|
||||
self.urls()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn urls(&self) -> Result<Vec<ice::url::Url>> {
|
||||
let mut urls = vec![];
|
||||
|
||||
for url_str in &self.urls {
|
||||
let mut url = self.parse_url(url_str)?;
|
||||
if url.scheme == ice::url::SchemeType::Turn || url.scheme == ice::url::SchemeType::Turns
|
||||
{
|
||||
// https://www.w3.org/TR/webrtc/#set-the-configuration (step #11.3.2)
|
||||
if self.username.is_empty() || self.credential.is_empty() {
|
||||
return Err(Error::ErrNoTurnCredentials);
|
||||
}
|
||||
url.username = self.username.clone();
|
||||
|
||||
match self.credential_type {
|
||||
RTCIceCredentialType::Password => {
|
||||
// https://www.w3.org/TR/webrtc/#set-the-configuration (step #11.3.3)
|
||||
url.password = self.credential.clone();
|
||||
}
|
||||
RTCIceCredentialType::Oauth => {
|
||||
// https://www.w3.org/TR/webrtc/#set-the-configuration (step #11.3.4)
|
||||
/*if _, ok: = s.Credential.(OAuthCredential); !ok {
|
||||
return nil,
|
||||
&rtcerr.InvalidAccessError{Err: ErrTurnCredentials
|
||||
}
|
||||
}*/
|
||||
}
|
||||
_ => return Err(Error::ErrTurnCredentials),
|
||||
};
|
||||
}
|
||||
|
||||
urls.push(url);
|
||||
}
|
||||
|
||||
Ok(urls)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_ice_server_validate_success() {
|
||||
let tests = vec![
|
||||
(
|
||||
RTCIceServer {
|
||||
urls: vec!["turn:192.158.29.39?transport=udp".to_owned()],
|
||||
username: "unittest".to_owned(),
|
||||
credential: "placeholder".to_owned(),
|
||||
credential_type: RTCIceCredentialType::Password,
|
||||
},
|
||||
true,
|
||||
),
|
||||
(
|
||||
RTCIceServer {
|
||||
urls: vec!["turn:[2001:db8:1234:5678::1]?transport=udp".to_owned()],
|
||||
username: "unittest".to_owned(),
|
||||
credential: "placeholder".to_owned(),
|
||||
credential_type: RTCIceCredentialType::Password,
|
||||
},
|
||||
true,
|
||||
),
|
||||
/*TODO:(ICEServer{
|
||||
URLs: []string{"turn:192.158.29.39?transport=udp"},
|
||||
Username: "unittest".to_owned(),
|
||||
Credential: OAuthCredential{
|
||||
MACKey: "WmtzanB3ZW9peFhtdm42NzUzNG0=",
|
||||
AccessToken: "AAwg3kPHWPfvk9bDFL936wYvkoctMADzQ5VhNDgeMR3+ZlZ35byg972fW8QjpEl7bx91YLBPFsIhsxloWcXPhA==",
|
||||
},
|
||||
CredentialType: ICECredentialTypeOauth,
|
||||
}, true),*/
|
||||
];
|
||||
|
||||
for (ice_server, expected_validate) in tests {
|
||||
let result = ice_server.urls();
|
||||
assert_eq!(result.is_ok(), expected_validate);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ice_server_validate_failure() {
|
||||
let tests = vec![
|
||||
(
|
||||
RTCIceServer {
|
||||
urls: vec!["turn:192.158.29.39?transport=udp".to_owned()],
|
||||
..Default::default()
|
||||
},
|
||||
Error::ErrNoTurnCredentials,
|
||||
),
|
||||
(
|
||||
RTCIceServer {
|
||||
urls: vec!["turn:192.158.29.39?transport=udp".to_owned()],
|
||||
username: "unittest".to_owned(),
|
||||
credential: String::new(),
|
||||
credential_type: RTCIceCredentialType::Password,
|
||||
},
|
||||
Error::ErrNoTurnCredentials,
|
||||
),
|
||||
(
|
||||
RTCIceServer {
|
||||
urls: vec!["turn:192.158.29.39?transport=udp".to_owned()],
|
||||
username: "unittest".to_owned(),
|
||||
credential: String::new(),
|
||||
credential_type: RTCIceCredentialType::Oauth,
|
||||
},
|
||||
Error::ErrNoTurnCredentials,
|
||||
),
|
||||
(
|
||||
RTCIceServer {
|
||||
urls: vec!["turn:192.158.29.39?transport=udp".to_owned()],
|
||||
username: "unittest".to_owned(),
|
||||
credential: String::new(),
|
||||
credential_type: RTCIceCredentialType::Unspecified,
|
||||
},
|
||||
Error::ErrNoTurnCredentials,
|
||||
),
|
||||
];
|
||||
|
||||
for (ice_server, expected_err) in tests {
|
||||
if let Err(err) = ice_server.urls() {
|
||||
assert_eq!(err, expected_err, "{ice_server:?} with err {err:?}");
|
||||
} else {
|
||||
panic!("expected error, but got ok");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ice_server_validate_failure_err_stun_query() {
|
||||
let tests = vec![(
|
||||
RTCIceServer {
|
||||
urls: vec!["stun:google.de?transport=udp".to_owned()],
|
||||
username: "unittest".to_owned(),
|
||||
credential: String::new(),
|
||||
credential_type: RTCIceCredentialType::Oauth,
|
||||
},
|
||||
ice::Error::ErrStunQuery,
|
||||
)];
|
||||
|
||||
for (ice_server, expected_err) in tests {
|
||||
if let Err(err) = ice_server.urls() {
|
||||
assert_eq!(err, expected_err, "{ice_server:?} with err {err:?}");
|
||||
} else {
|
||||
panic!("expected error, but got ok");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
use std::fmt;
|
||||
|
||||
use ice::state::ConnectionState;
|
||||
|
||||
/// ICETransportState represents the current state of the ICE transport.
|
||||
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub enum RTCIceTransportState {
|
||||
#[default]
|
||||
Unspecified,
|
||||
|
||||
/// ICETransportStateNew indicates the ICETransport is waiting
|
||||
/// for remote candidates to be supplied.
|
||||
New,
|
||||
|
||||
/// ICETransportStateChecking indicates the ICETransport has
|
||||
/// received at least one remote candidate, and a local and remote
|
||||
/// ICECandidateComplete dictionary was not added as the last candidate.
|
||||
Checking,
|
||||
|
||||
/// ICETransportStateConnected indicates the ICETransport has
|
||||
/// received a response to an outgoing connectivity check, or has
|
||||
/// received incoming DTLS/media after a successful response to an
|
||||
/// incoming connectivity check, but is still checking other candidate
|
||||
/// pairs to see if there is a better connection.
|
||||
Connected,
|
||||
|
||||
/// ICETransportStateCompleted indicates the ICETransport tested
|
||||
/// all appropriate candidate pairs and at least one functioning
|
||||
/// candidate pair has been found.
|
||||
Completed,
|
||||
|
||||
/// ICETransportStateFailed indicates the ICETransport the last
|
||||
/// candidate was added and all appropriate candidate pairs have either
|
||||
/// failed connectivity checks or have lost consent.
|
||||
Failed,
|
||||
|
||||
/// ICETransportStateDisconnected indicates the ICETransport has received
|
||||
/// at least one local and remote candidate, but the final candidate was
|
||||
/// received yet and all appropriate candidate pairs thus far have been
|
||||
/// tested and failed.
|
||||
Disconnected,
|
||||
|
||||
/// ICETransportStateClosed indicates the ICETransport has shut down
|
||||
/// and is no longer responding to STUN requests.
|
||||
Closed,
|
||||
}
|
||||
|
||||
const ICE_TRANSPORT_STATE_NEW_STR: &str = "new";
|
||||
const ICE_TRANSPORT_STATE_CHECKING_STR: &str = "checking";
|
||||
const ICE_TRANSPORT_STATE_CONNECTED_STR: &str = "connected";
|
||||
const ICE_TRANSPORT_STATE_COMPLETED_STR: &str = "completed";
|
||||
const ICE_TRANSPORT_STATE_FAILED_STR: &str = "failed";
|
||||
const ICE_TRANSPORT_STATE_DISCONNECTED_STR: &str = "disconnected";
|
||||
const ICE_TRANSPORT_STATE_CLOSED_STR: &str = "closed";
|
||||
|
||||
impl From<&str> for RTCIceTransportState {
|
||||
fn from(raw: &str) -> Self {
|
||||
match raw {
|
||||
ICE_TRANSPORT_STATE_NEW_STR => RTCIceTransportState::New,
|
||||
ICE_TRANSPORT_STATE_CHECKING_STR => RTCIceTransportState::Checking,
|
||||
ICE_TRANSPORT_STATE_CONNECTED_STR => RTCIceTransportState::Connected,
|
||||
ICE_TRANSPORT_STATE_COMPLETED_STR => RTCIceTransportState::Completed,
|
||||
ICE_TRANSPORT_STATE_FAILED_STR => RTCIceTransportState::Failed,
|
||||
ICE_TRANSPORT_STATE_DISCONNECTED_STR => RTCIceTransportState::Disconnected,
|
||||
ICE_TRANSPORT_STATE_CLOSED_STR => RTCIceTransportState::Closed,
|
||||
_ => RTCIceTransportState::Unspecified,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u8> for RTCIceTransportState {
|
||||
fn from(v: u8) -> Self {
|
||||
match v {
|
||||
1 => Self::New,
|
||||
2 => Self::Checking,
|
||||
3 => Self::Connected,
|
||||
4 => Self::Completed,
|
||||
5 => Self::Failed,
|
||||
6 => Self::Disconnected,
|
||||
7 => Self::Closed,
|
||||
_ => Self::Unspecified,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for RTCIceTransportState {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {
|
||||
RTCIceTransportState::New => write!(f, "{ICE_TRANSPORT_STATE_NEW_STR}"),
|
||||
RTCIceTransportState::Checking => write!(f, "{ICE_TRANSPORT_STATE_CHECKING_STR}"),
|
||||
RTCIceTransportState::Connected => {
|
||||
write!(f, "{ICE_TRANSPORT_STATE_CONNECTED_STR}")
|
||||
}
|
||||
RTCIceTransportState::Completed => write!(f, "{ICE_TRANSPORT_STATE_COMPLETED_STR}"),
|
||||
RTCIceTransportState::Failed => {
|
||||
write!(f, "{ICE_TRANSPORT_STATE_FAILED_STR}")
|
||||
}
|
||||
RTCIceTransportState::Disconnected => {
|
||||
write!(f, "{ICE_TRANSPORT_STATE_DISCONNECTED_STR}")
|
||||
}
|
||||
RTCIceTransportState::Closed => {
|
||||
write!(f, "{ICE_TRANSPORT_STATE_CLOSED_STR}")
|
||||
}
|
||||
_ => write!(f, "{}", crate::UNSPECIFIED_STR),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ConnectionState> for RTCIceTransportState {
|
||||
fn from(raw: ConnectionState) -> Self {
|
||||
match raw {
|
||||
ConnectionState::New => RTCIceTransportState::New,
|
||||
ConnectionState::Checking => RTCIceTransportState::Checking,
|
||||
ConnectionState::Connected => RTCIceTransportState::Connected,
|
||||
ConnectionState::Completed => RTCIceTransportState::Completed,
|
||||
ConnectionState::Failed => RTCIceTransportState::Failed,
|
||||
ConnectionState::Disconnected => RTCIceTransportState::Disconnected,
|
||||
ConnectionState::Closed => RTCIceTransportState::Closed,
|
||||
_ => RTCIceTransportState::Unspecified,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RTCIceTransportState {
|
||||
pub(crate) fn to_ice(self) -> ConnectionState {
|
||||
match self {
|
||||
RTCIceTransportState::New => ConnectionState::New,
|
||||
RTCIceTransportState::Checking => ConnectionState::Checking,
|
||||
RTCIceTransportState::Connected => ConnectionState::Connected,
|
||||
RTCIceTransportState::Completed => ConnectionState::Completed,
|
||||
RTCIceTransportState::Failed => ConnectionState::Failed,
|
||||
RTCIceTransportState::Disconnected => ConnectionState::Disconnected,
|
||||
RTCIceTransportState::Closed => ConnectionState::Closed,
|
||||
_ => ConnectionState::Unspecified,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_ice_transport_state_string() {
|
||||
let tests = vec![
|
||||
(RTCIceTransportState::Unspecified, "Unspecified"),
|
||||
(RTCIceTransportState::New, "new"),
|
||||
(RTCIceTransportState::Checking, "checking"),
|
||||
(RTCIceTransportState::Connected, "connected"),
|
||||
(RTCIceTransportState::Completed, "completed"),
|
||||
(RTCIceTransportState::Failed, "failed"),
|
||||
(RTCIceTransportState::Disconnected, "disconnected"),
|
||||
(RTCIceTransportState::Closed, "closed"),
|
||||
];
|
||||
|
||||
for (state, expected_string) in tests {
|
||||
assert_eq!(state.to_string(), expected_string);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ice_transport_state_convert() {
|
||||
let tests = vec![
|
||||
(
|
||||
RTCIceTransportState::Unspecified,
|
||||
ConnectionState::Unspecified,
|
||||
),
|
||||
(RTCIceTransportState::New, ConnectionState::New),
|
||||
(RTCIceTransportState::Checking, ConnectionState::Checking),
|
||||
(RTCIceTransportState::Connected, ConnectionState::Connected),
|
||||
(RTCIceTransportState::Completed, ConnectionState::Completed),
|
||||
(RTCIceTransportState::Failed, ConnectionState::Failed),
|
||||
(
|
||||
RTCIceTransportState::Disconnected,
|
||||
ConnectionState::Disconnected,
|
||||
),
|
||||
(RTCIceTransportState::Closed, ConnectionState::Closed),
|
||||
];
|
||||
|
||||
for (native, ice_state) in tests {
|
||||
assert_eq!(native.to_ice(), ice_state);
|
||||
assert_eq!(native, RTCIceTransportState::from(ice_state));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
use std::sync::atomic::AtomicU32;
|
||||
|
||||
use tokio::time::Duration;
|
||||
use waitgroup::WaitGroup;
|
||||
|
||||
use super::*;
|
||||
use crate::api::media_engine::MediaEngine;
|
||||
use crate::api::APIBuilder;
|
||||
use crate::error::Result;
|
||||
use crate::ice_transport::ice_connection_state::RTCIceConnectionState;
|
||||
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,
|
||||
};
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_ice_transport_on_selected_candidate_pair_change() -> Result<()> {
|
||||
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?;
|
||||
|
||||
let (ice_complete_tx, mut ice_complete_rx) = mpsc::channel::<()>(1);
|
||||
let ice_complete_tx = Arc::new(Mutex::new(Some(ice_complete_tx)));
|
||||
pc_answer.on_ice_connection_state_change(Box::new(move |ice_state: RTCIceConnectionState| {
|
||||
let ice_complete_tx2 = Arc::clone(&ice_complete_tx);
|
||||
Box::pin(async move {
|
||||
if ice_state == RTCIceConnectionState::Connected {
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
let mut done = ice_complete_tx2.lock().await;
|
||||
done.take();
|
||||
}
|
||||
})
|
||||
}));
|
||||
|
||||
let sender_called_candidate_change = Arc::new(AtomicU32::new(0));
|
||||
let sender_called_candidate_change2 = Arc::clone(&sender_called_candidate_change);
|
||||
pc_offer
|
||||
.sctp()
|
||||
.transport()
|
||||
.ice_transport()
|
||||
.on_selected_candidate_pair_change(Box::new(move |_: RTCIceCandidatePair| {
|
||||
sender_called_candidate_change2.store(1, Ordering::SeqCst);
|
||||
Box::pin(async {})
|
||||
}));
|
||||
|
||||
signal_pair(&mut pc_offer, &mut pc_answer).await?;
|
||||
|
||||
let _ = ice_complete_rx.recv().await;
|
||||
assert_eq!(
|
||||
sender_called_candidate_change.load(Ordering::SeqCst),
|
||||
1,
|
||||
"Sender ICETransport OnSelectedCandidateChange was never called"
|
||||
);
|
||||
|
||||
close_pair_now(&pc_offer, &pc_answer).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_ice_transport_get_selected_candidate_pair() -> Result<()> {
|
||||
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?;
|
||||
|
||||
let peer_connection_connected = WaitGroup::new();
|
||||
until_connection_state(
|
||||
&mut offerer,
|
||||
&peer_connection_connected,
|
||||
RTCPeerConnectionState::Connected,
|
||||
)
|
||||
.await;
|
||||
until_connection_state(
|
||||
&mut answerer,
|
||||
&peer_connection_connected,
|
||||
RTCPeerConnectionState::Connected,
|
||||
)
|
||||
.await;
|
||||
|
||||
let offerer_selected_pair = offerer
|
||||
.sctp()
|
||||
.transport()
|
||||
.ice_transport()
|
||||
.get_selected_candidate_pair()
|
||||
.await;
|
||||
assert!(offerer_selected_pair.is_none());
|
||||
|
||||
let answerer_selected_pair = answerer
|
||||
.sctp()
|
||||
.transport()
|
||||
.ice_transport()
|
||||
.get_selected_candidate_pair()
|
||||
.await;
|
||||
assert!(answerer_selected_pair.is_none());
|
||||
|
||||
signal_pair(&mut offerer, &mut answerer).await?;
|
||||
|
||||
peer_connection_connected.wait().await;
|
||||
|
||||
let offerer_selected_pair = offerer
|
||||
.sctp()
|
||||
.transport()
|
||||
.ice_transport()
|
||||
.get_selected_candidate_pair()
|
||||
.await;
|
||||
assert!(offerer_selected_pair.is_some());
|
||||
|
||||
let answerer_selected_pair = answerer
|
||||
.sctp()
|
||||
.transport()
|
||||
.ice_transport()
|
||||
.get_selected_candidate_pair()
|
||||
.await;
|
||||
assert!(answerer_selected_pair.is_some());
|
||||
|
||||
close_pair_now(&offerer, &answerer).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
+355
@@ -0,0 +1,355 @@
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::sync::atomic::{AtomicU8, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
use arc_swap::ArcSwapOption;
|
||||
use ice::candidate::Candidate;
|
||||
use ice::state::ConnectionState;
|
||||
use ice_candidate::RTCIceCandidate;
|
||||
use ice_candidate_pair::RTCIceCandidatePair;
|
||||
use ice_gatherer::RTCIceGatherer;
|
||||
use ice_role::RTCIceRole;
|
||||
use tokio::sync::{mpsc, Mutex};
|
||||
use util::Conn;
|
||||
|
||||
use crate::error::{flatten_errs, Error, Result};
|
||||
use crate::ice_transport::ice_parameters::RTCIceParameters;
|
||||
use crate::ice_transport::ice_transport_state::RTCIceTransportState;
|
||||
use crate::mux::endpoint::Endpoint;
|
||||
use crate::mux::mux_func::MatchFunc;
|
||||
use crate::mux::{Config, Mux};
|
||||
use crate::stats::stats_collector::StatsCollector;
|
||||
use crate::stats::ICETransportStats;
|
||||
use crate::stats::StatsReportType::Transport;
|
||||
|
||||
#[cfg(test)]
|
||||
mod ice_transport_test;
|
||||
|
||||
pub mod ice_candidate;
|
||||
pub mod ice_candidate_pair;
|
||||
pub mod ice_candidate_type;
|
||||
pub mod ice_connection_state;
|
||||
pub mod ice_credential_type;
|
||||
pub mod ice_gatherer;
|
||||
pub mod ice_gatherer_state;
|
||||
pub mod ice_gathering_state;
|
||||
pub mod ice_parameters;
|
||||
pub mod ice_protocol;
|
||||
pub mod ice_role;
|
||||
pub mod ice_server;
|
||||
pub mod ice_transport_state;
|
||||
|
||||
pub type OnConnectionStateChangeHdlrFn = Box<
|
||||
dyn (FnMut(RTCIceTransportState) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>)
|
||||
+ Send
|
||||
+ Sync,
|
||||
>;
|
||||
|
||||
pub type OnSelectedCandidatePairChangeHdlrFn = Box<
|
||||
dyn (FnMut(RTCIceCandidatePair) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>)
|
||||
+ Send
|
||||
+ Sync,
|
||||
>;
|
||||
|
||||
#[derive(Default)]
|
||||
struct ICETransportInternal {
|
||||
role: RTCIceRole,
|
||||
conn: Option<Arc<dyn Conn + Send + Sync>>, //AgentConn
|
||||
mux: Option<Mux>,
|
||||
cancel_tx: Option<mpsc::Sender<()>>,
|
||||
}
|
||||
|
||||
/// ICETransport allows an application access to information about the ICE
|
||||
/// transport over which packets are sent and received.
|
||||
#[derive(Default)]
|
||||
pub struct RTCIceTransport {
|
||||
pub(crate) gatherer: Arc<RTCIceGatherer>,
|
||||
on_connection_state_change_handler: Arc<ArcSwapOption<Mutex<OnConnectionStateChangeHdlrFn>>>,
|
||||
on_selected_candidate_pair_change_handler:
|
||||
Arc<ArcSwapOption<Mutex<OnSelectedCandidatePairChangeHdlrFn>>>,
|
||||
state: Arc<AtomicU8>, // ICETransportState
|
||||
internal: Mutex<ICETransportInternal>,
|
||||
}
|
||||
|
||||
impl RTCIceTransport {
|
||||
/// creates a new new_icetransport.
|
||||
pub(crate) fn new(gatherer: Arc<RTCIceGatherer>) -> Self {
|
||||
RTCIceTransport {
|
||||
state: Arc::new(AtomicU8::new(RTCIceTransportState::New as u8)),
|
||||
gatherer,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// get_selected_candidate_pair returns the selected candidate pair on which packets are sent
|
||||
/// if there is no selected pair nil is returned
|
||||
pub async fn get_selected_candidate_pair(&self) -> Option<RTCIceCandidatePair> {
|
||||
if let Some(agent) = self.gatherer.get_agent().await {
|
||||
if let Some(ice_pair) = agent.get_selected_candidate_pair() {
|
||||
let local = RTCIceCandidate::from(&ice_pair.local);
|
||||
let remote = RTCIceCandidate::from(&ice_pair.remote);
|
||||
return Some(RTCIceCandidatePair::new(local, remote));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Start incoming connectivity checks based on its configured role.
|
||||
pub async fn start(&self, params: &RTCIceParameters, role: Option<RTCIceRole>) -> Result<()> {
|
||||
if self.state() != RTCIceTransportState::New {
|
||||
return Err(Error::ErrICETransportNotInNew);
|
||||
}
|
||||
|
||||
self.ensure_gatherer().await?;
|
||||
|
||||
if let Some(agent) = self.gatherer.get_agent().await {
|
||||
let state = Arc::clone(&self.state);
|
||||
|
||||
let on_connection_state_change_handler =
|
||||
Arc::clone(&self.on_connection_state_change_handler);
|
||||
agent.on_connection_state_change(Box::new(move |ice_state: ConnectionState| {
|
||||
let s = RTCIceTransportState::from(ice_state);
|
||||
let on_connection_state_change_handler_clone =
|
||||
Arc::clone(&on_connection_state_change_handler);
|
||||
state.store(s as u8, Ordering::SeqCst);
|
||||
Box::pin(async move {
|
||||
if let Some(handler) = &*on_connection_state_change_handler_clone.load() {
|
||||
let mut f = handler.lock().await;
|
||||
f(s).await;
|
||||
}
|
||||
})
|
||||
}));
|
||||
|
||||
let on_selected_candidate_pair_change_handler =
|
||||
Arc::clone(&self.on_selected_candidate_pair_change_handler);
|
||||
agent.on_selected_candidate_pair_change(Box::new(
|
||||
move |local: &Arc<dyn Candidate + Send + Sync>,
|
||||
remote: &Arc<dyn Candidate + Send + Sync>| {
|
||||
let on_selected_candidate_pair_change_handler_clone =
|
||||
Arc::clone(&on_selected_candidate_pair_change_handler);
|
||||
let local = RTCIceCandidate::from(local);
|
||||
let remote = RTCIceCandidate::from(remote);
|
||||
Box::pin(async move {
|
||||
if let Some(handler) =
|
||||
&*on_selected_candidate_pair_change_handler_clone.load()
|
||||
{
|
||||
let mut f = handler.lock().await;
|
||||
f(RTCIceCandidatePair::new(local, remote)).await;
|
||||
}
|
||||
})
|
||||
},
|
||||
));
|
||||
|
||||
let role = if let Some(role) = role {
|
||||
role
|
||||
} else {
|
||||
RTCIceRole::Controlled
|
||||
};
|
||||
|
||||
let (cancel_tx, cancel_rx) = mpsc::channel(1);
|
||||
{
|
||||
let mut internal = self.internal.lock().await;
|
||||
internal.role = role;
|
||||
internal.cancel_tx = Some(cancel_tx);
|
||||
}
|
||||
|
||||
let conn: Arc<dyn Conn + Send + Sync> = match role {
|
||||
RTCIceRole::Controlling => {
|
||||
agent
|
||||
.dial(
|
||||
cancel_rx,
|
||||
params.username_fragment.clone(),
|
||||
params.password.clone(),
|
||||
)
|
||||
.await?
|
||||
}
|
||||
|
||||
RTCIceRole::Controlled => {
|
||||
agent
|
||||
.accept(
|
||||
cancel_rx,
|
||||
params.username_fragment.clone(),
|
||||
params.password.clone(),
|
||||
)
|
||||
.await?
|
||||
}
|
||||
|
||||
_ => return Err(Error::ErrICERoleUnknown),
|
||||
};
|
||||
|
||||
let config = Config {
|
||||
conn: Arc::clone(&conn),
|
||||
buffer_size: self.gatherer.setting_engine.get_receive_mtu(),
|
||||
};
|
||||
|
||||
{
|
||||
let mut internal = self.internal.lock().await;
|
||||
internal.conn = Some(conn);
|
||||
internal.mux = Some(Mux::new(config));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::ErrICEAgentNotExist)
|
||||
}
|
||||
}
|
||||
|
||||
/// restart is not exposed currently because ORTC has users create a whole new ICETransport
|
||||
/// so for now lets keep it private so we don't cause ORTC users to depend on non-standard APIs
|
||||
pub(crate) async fn restart(&self) -> Result<()> {
|
||||
if let Some(agent) = self.gatherer.get_agent().await {
|
||||
agent
|
||||
.restart(
|
||||
self.gatherer
|
||||
.setting_engine
|
||||
.candidates
|
||||
.username_fragment
|
||||
.clone(),
|
||||
self.gatherer.setting_engine.candidates.password.clone(),
|
||||
)
|
||||
.await?;
|
||||
} else {
|
||||
return Err(Error::ErrICEAgentNotExist);
|
||||
}
|
||||
self.gatherer.gather().await
|
||||
}
|
||||
|
||||
/// Stop irreversibly stops the ICETransport.
|
||||
pub async fn stop(&self) -> Result<()> {
|
||||
self.set_state(RTCIceTransportState::Closed);
|
||||
|
||||
let mut errs: Vec<Error> = vec![];
|
||||
{
|
||||
let mut internal = self.internal.lock().await;
|
||||
internal.cancel_tx.take();
|
||||
if let Some(mut mux) = internal.mux.take() {
|
||||
mux.close().await;
|
||||
}
|
||||
if let Some(conn) = internal.conn.take() {
|
||||
if let Err(err) = conn.close().await {
|
||||
errs.push(err.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(err) = self.gatherer.close().await {
|
||||
errs.push(err);
|
||||
}
|
||||
|
||||
flatten_errs(errs)
|
||||
}
|
||||
|
||||
/// on_selected_candidate_pair_change sets a handler that is invoked when a new
|
||||
/// ICE candidate pair is selected
|
||||
pub fn on_selected_candidate_pair_change(&self, f: OnSelectedCandidatePairChangeHdlrFn) {
|
||||
self.on_selected_candidate_pair_change_handler
|
||||
.store(Some(Arc::new(Mutex::new(f))));
|
||||
}
|
||||
|
||||
/// on_connection_state_change sets a handler that is fired when the ICE
|
||||
/// connection state changes.
|
||||
pub fn on_connection_state_change(&self, f: OnConnectionStateChangeHdlrFn) {
|
||||
self.on_connection_state_change_handler
|
||||
.store(Some(Arc::new(Mutex::new(f))));
|
||||
}
|
||||
|
||||
/// Role indicates the current role of the ICE transport.
|
||||
pub async fn role(&self) -> RTCIceRole {
|
||||
let internal = self.internal.lock().await;
|
||||
internal.role
|
||||
}
|
||||
|
||||
/// set_remote_candidates sets the sequence of candidates associated with the remote ICETransport.
|
||||
pub async fn set_remote_candidates(&self, remote_candidates: &[RTCIceCandidate]) -> Result<()> {
|
||||
self.ensure_gatherer().await?;
|
||||
|
||||
if let Some(agent) = self.gatherer.get_agent().await {
|
||||
for rc in remote_candidates {
|
||||
let c: Arc<dyn Candidate + Send + Sync> = Arc::new(rc.to_ice()?);
|
||||
agent.add_remote_candidate(&c)?;
|
||||
}
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::ErrICEAgentNotExist)
|
||||
}
|
||||
}
|
||||
|
||||
/// adds a candidate associated with the remote ICETransport.
|
||||
pub async fn add_remote_candidate(
|
||||
&self,
|
||||
remote_candidate: Option<RTCIceCandidate>,
|
||||
) -> Result<()> {
|
||||
self.ensure_gatherer().await?;
|
||||
|
||||
if let Some(agent) = self.gatherer.get_agent().await {
|
||||
if let Some(r) = remote_candidate {
|
||||
let c: Arc<dyn Candidate + Send + Sync> = Arc::new(r.to_ice()?);
|
||||
agent.add_remote_candidate(&c)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::ErrICEAgentNotExist)
|
||||
}
|
||||
}
|
||||
|
||||
/// State returns the current ice transport state.
|
||||
pub fn state(&self) -> RTCIceTransportState {
|
||||
RTCIceTransportState::from(self.state.load(Ordering::SeqCst))
|
||||
}
|
||||
|
||||
pub(crate) fn set_state(&self, s: RTCIceTransportState) {
|
||||
self.state.store(s as u8, Ordering::SeqCst)
|
||||
}
|
||||
|
||||
pub(crate) async fn new_endpoint(&self, f: MatchFunc) -> Option<Arc<Endpoint>> {
|
||||
let internal = self.internal.lock().await;
|
||||
if let Some(mux) = &internal.mux {
|
||||
Some(mux.new_endpoint(f).await)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn ensure_gatherer(&self) -> Result<()> {
|
||||
if self.gatherer.get_agent().await.is_none() {
|
||||
self.gatherer.create_agent().await
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn collect_stats(&self, collector: &StatsCollector) {
|
||||
if let Some(agent) = self.gatherer.get_agent().await {
|
||||
let stats = ICETransportStats::new("ice_transport".to_string(), agent);
|
||||
|
||||
collector.insert("ice_transport".to_string(), Transport(stats));
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn have_remote_credentials_change(
|
||||
&self,
|
||||
new_ufrag: &str,
|
||||
new_pwd: &str,
|
||||
) -> bool {
|
||||
if let Some(agent) = self.gatherer.get_agent().await {
|
||||
let (ufrag, upwd) = agent.get_remote_user_credentials().await;
|
||||
ufrag != new_ufrag || upwd != new_pwd
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn set_remote_credentials(
|
||||
&self,
|
||||
new_ufrag: String,
|
||||
new_pwd: String,
|
||||
) -> Result<()> {
|
||||
if let Some(agent) = self.gatherer.get_agent().await {
|
||||
Ok(agent.set_remote_credentials(new_ufrag, new_pwd).await?)
|
||||
} else {
|
||||
Err(Error::ErrICEAgentNotExist)
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+33
@@ -0,0 +1,33 @@
|
||||
#![warn(rust_2018_idioms)]
|
||||
#![allow(dead_code)]
|
||||
|
||||
// re-export sub-crates
|
||||
pub use {data, dtls, ice, interceptor, mdns, media, rtcp, rtp, sctp, sdp, srtp, stun, turn, util};
|
||||
|
||||
pub mod api;
|
||||
pub mod data_channel;
|
||||
pub mod dtls_transport;
|
||||
pub mod error;
|
||||
pub mod ice_transport;
|
||||
pub mod mux;
|
||||
pub mod peer_connection;
|
||||
pub mod rtp_transceiver;
|
||||
pub mod sctp_transport;
|
||||
pub mod stats;
|
||||
pub mod track;
|
||||
|
||||
pub use error::Error;
|
||||
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
pub(crate) const UNSPECIFIED_STR: &str = "Unspecified";
|
||||
|
||||
/// Equal to UDP MTU
|
||||
pub(crate) const RECEIVE_MTU: usize = 1460;
|
||||
|
||||
pub(crate) const SDP_ATTRIBUTE_RID: &str = "rid";
|
||||
pub(crate) const SDP_ATTRIBUTE_SIMULCAST: &str = "simulcast";
|
||||
pub(crate) const GENERATED_CERTIFICATE_ORIGIN: &str = "WebRTC";
|
||||
pub(crate) const SDES_REPAIR_RTP_STREAM_ID_URI: &str =
|
||||
"urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id";
|
||||
Vendored
+73
@@ -0,0 +1,73 @@
|
||||
use std::collections::HashMap;
|
||||
use std::io;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use tokio::sync::Mutex;
|
||||
use util::{Buffer, Conn};
|
||||
|
||||
use crate::mux::mux_func::MatchFunc;
|
||||
|
||||
/// Endpoint implements net.Conn. It is used to read muxed packets.
|
||||
pub struct Endpoint {
|
||||
pub(crate) id: usize,
|
||||
pub(crate) buffer: Buffer,
|
||||
pub(crate) match_fn: MatchFunc,
|
||||
pub(crate) next_conn: Arc<dyn Conn + Send + Sync>,
|
||||
pub(crate) endpoints: Arc<Mutex<HashMap<usize, Arc<Endpoint>>>>,
|
||||
}
|
||||
|
||||
impl Endpoint {
|
||||
/// Close unregisters the endpoint from the Mux
|
||||
pub async fn close(&self) -> Result<()> {
|
||||
self.buffer.close().await;
|
||||
|
||||
let mut endpoints = self.endpoints.lock().await;
|
||||
endpoints.remove(&self.id);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
type Result<T> = std::result::Result<T, util::Error>;
|
||||
|
||||
#[async_trait]
|
||||
impl Conn for Endpoint {
|
||||
async fn connect(&self, _addr: SocketAddr) -> Result<()> {
|
||||
Err(io::Error::new(io::ErrorKind::Other, "Not applicable").into())
|
||||
}
|
||||
|
||||
/// reads a packet of len(p) bytes from the underlying conn
|
||||
/// that are matched by the associated MuxFunc
|
||||
async fn recv(&self, buf: &mut [u8]) -> Result<usize> {
|
||||
match self.buffer.read(buf, None).await {
|
||||
Ok(n) => Ok(n),
|
||||
Err(err) => Err(io::Error::new(io::ErrorKind::Other, err.to_string()).into()),
|
||||
}
|
||||
}
|
||||
async fn recv_from(&self, _buf: &mut [u8]) -> Result<(usize, SocketAddr)> {
|
||||
Err(io::Error::new(io::ErrorKind::Other, "Not applicable").into())
|
||||
}
|
||||
|
||||
/// writes bytes to the underlying conn
|
||||
async fn send(&self, buf: &[u8]) -> Result<usize> {
|
||||
self.next_conn.send(buf).await
|
||||
}
|
||||
|
||||
async fn send_to(&self, _buf: &[u8], _target: SocketAddr) -> Result<usize> {
|
||||
Err(io::Error::new(io::ErrorKind::Other, "Not applicable").into())
|
||||
}
|
||||
|
||||
fn local_addr(&self) -> Result<SocketAddr> {
|
||||
self.next_conn.local_addr()
|
||||
}
|
||||
|
||||
fn remote_addr(&self) -> Option<SocketAddr> {
|
||||
self.next_conn.remote_addr()
|
||||
}
|
||||
|
||||
async fn close(&self) -> Result<()> {
|
||||
self.next_conn.close().await
|
||||
}
|
||||
}
|
||||
Vendored
+156
@@ -0,0 +1,156 @@
|
||||
#[cfg(test)]
|
||||
mod mux_test;
|
||||
|
||||
pub mod endpoint;
|
||||
pub mod mux_func;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
use tokio::sync::{mpsc, Mutex};
|
||||
use util::{Buffer, Conn};
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::mux::endpoint::Endpoint;
|
||||
use crate::mux::mux_func::MatchFunc;
|
||||
use crate::util::Error;
|
||||
|
||||
/// mux multiplexes packets on a single socket (RFC7983)
|
||||
|
||||
/// The maximum amount of data that can be buffered before returning errors.
|
||||
const MAX_BUFFER_SIZE: usize = 1000 * 1000; // 1MB
|
||||
|
||||
/// Config collects the arguments to mux.Mux construction into
|
||||
/// a single structure
|
||||
pub struct Config {
|
||||
pub conn: Arc<dyn Conn + Send + Sync>,
|
||||
pub buffer_size: usize,
|
||||
}
|
||||
|
||||
/// Mux allows multiplexing
|
||||
#[derive(Clone)]
|
||||
pub struct Mux {
|
||||
id: Arc<AtomicUsize>,
|
||||
next_conn: Arc<dyn Conn + Send + Sync>,
|
||||
endpoints: Arc<Mutex<HashMap<usize, Arc<Endpoint>>>>,
|
||||
buffer_size: usize,
|
||||
closed_ch_tx: Option<mpsc::Sender<()>>,
|
||||
}
|
||||
|
||||
impl Mux {
|
||||
pub fn new(config: Config) -> Self {
|
||||
let (closed_ch_tx, closed_ch_rx) = mpsc::channel(1);
|
||||
let m = Mux {
|
||||
id: Arc::new(AtomicUsize::new(0)),
|
||||
next_conn: Arc::clone(&config.conn),
|
||||
endpoints: Arc::new(Mutex::new(HashMap::new())),
|
||||
buffer_size: config.buffer_size,
|
||||
closed_ch_tx: Some(closed_ch_tx),
|
||||
};
|
||||
|
||||
let buffer_size = m.buffer_size;
|
||||
let next_conn = Arc::clone(&m.next_conn);
|
||||
let endpoints = Arc::clone(&m.endpoints);
|
||||
tokio::spawn(async move {
|
||||
Mux::read_loop(buffer_size, next_conn, closed_ch_rx, endpoints).await;
|
||||
});
|
||||
|
||||
m
|
||||
}
|
||||
|
||||
/// creates a new Endpoint
|
||||
pub async fn new_endpoint(&self, f: MatchFunc) -> Arc<Endpoint> {
|
||||
let mut endpoints = self.endpoints.lock().await;
|
||||
|
||||
let id = self.id.fetch_add(1, Ordering::SeqCst);
|
||||
// Set a maximum size of the buffer in bytes.
|
||||
let e = Arc::new(Endpoint {
|
||||
id,
|
||||
buffer: Buffer::new(0, MAX_BUFFER_SIZE),
|
||||
match_fn: f,
|
||||
next_conn: Arc::clone(&self.next_conn),
|
||||
endpoints: Arc::clone(&self.endpoints),
|
||||
});
|
||||
|
||||
endpoints.insert(e.id, Arc::clone(&e));
|
||||
|
||||
e
|
||||
}
|
||||
|
||||
/// remove_endpoint removes an endpoint from the Mux
|
||||
pub async fn remove_endpoint(&mut self, e: &Endpoint) {
|
||||
let mut endpoints = self.endpoints.lock().await;
|
||||
endpoints.remove(&e.id);
|
||||
}
|
||||
|
||||
/// Close closes the Mux and all associated Endpoints.
|
||||
pub async fn close(&mut self) {
|
||||
self.closed_ch_tx.take();
|
||||
|
||||
let mut endpoints = self.endpoints.lock().await;
|
||||
endpoints.clear();
|
||||
}
|
||||
|
||||
async fn read_loop(
|
||||
buffer_size: usize,
|
||||
next_conn: Arc<dyn Conn + Send + Sync>,
|
||||
mut closed_ch_rx: mpsc::Receiver<()>,
|
||||
endpoints: Arc<Mutex<HashMap<usize, Arc<Endpoint>>>>,
|
||||
) {
|
||||
let mut buf = vec![0u8; buffer_size];
|
||||
let mut n = 0usize;
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = closed_ch_rx.recv() => break,
|
||||
result = next_conn.recv(&mut buf) => {
|
||||
if let Ok(m) = result{
|
||||
n = m;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if let Err(err) = Mux::dispatch(&buf[..n], &endpoints).await {
|
||||
log::error!("mux: ending readLoop dispatch error {:?}", err);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn dispatch(
|
||||
buf: &[u8],
|
||||
endpoints: &Arc<Mutex<HashMap<usize, Arc<Endpoint>>>>,
|
||||
) -> Result<()> {
|
||||
let mut endpoint = None;
|
||||
|
||||
{
|
||||
let eps = endpoints.lock().await;
|
||||
for ep in eps.values() {
|
||||
if (ep.match_fn)(buf) {
|
||||
endpoint = Some(Arc::clone(ep));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ep) = endpoint {
|
||||
match ep.buffer.write(buf).await {
|
||||
// Expected when bytes are received faster than the endpoint can process them
|
||||
Err(Error::ErrBufferFull) => {
|
||||
log::info!("mux: endpoint buffer is full, dropping packet")
|
||||
}
|
||||
Ok(_) => (),
|
||||
Err(e) => return Err(crate::Error::Util(e)),
|
||||
}
|
||||
} else if !buf.is_empty() {
|
||||
log::warn!(
|
||||
"Warning: mux: no endpoint for packet starting with {}",
|
||||
buf[0]
|
||||
);
|
||||
} else {
|
||||
log::warn!("Warning: mux: no endpoint for zero length packet");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Vendored
+63
@@ -0,0 +1,63 @@
|
||||
/// MatchFunc allows custom logic for mapping packets to an Endpoint
|
||||
pub type MatchFunc = Box<dyn (Fn(&[u8]) -> bool) + Send + Sync>;
|
||||
|
||||
/// match_all always returns true
|
||||
pub fn match_all(_b: &[u8]) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
/// match_range is a MatchFunc that accepts packets with the first byte in [lower..upper]
|
||||
pub fn match_range(lower: u8, upper: u8) -> MatchFunc {
|
||||
Box::new(move |buf: &[u8]| -> bool {
|
||||
if buf.is_empty() {
|
||||
return false;
|
||||
}
|
||||
let b = buf[0];
|
||||
b >= lower && b <= upper
|
||||
})
|
||||
}
|
||||
|
||||
/// MatchFuncs as described in RFC7983
|
||||
/// <https://tools.ietf.org/html/rfc7983>
|
||||
/// +----------------+
|
||||
/// | [0..3] -+--> forward to STUN
|
||||
/// | |
|
||||
/// | [16..19] -+--> forward to ZRTP
|
||||
/// | |
|
||||
/// packet --> | [20..63] -+--> forward to DTLS
|
||||
/// | |
|
||||
/// | [64..79] -+--> forward to TURN Channel
|
||||
/// | |
|
||||
/// | [128..191] -+--> forward to RTP/RTCP
|
||||
/// +----------------+
|
||||
/// match_dtls is a MatchFunc that accepts packets with the first byte in [20..63]
|
||||
/// as defied in RFC7983
|
||||
pub fn match_dtls(b: &[u8]) -> bool {
|
||||
match_range(20, 63)(b)
|
||||
}
|
||||
|
||||
// match_srtp_or_srtcp is a MatchFunc that accepts packets with the first byte in [128..191]
|
||||
// as defied in RFC7983
|
||||
pub fn match_srtp_or_srtcp(b: &[u8]) -> bool {
|
||||
match_range(128, 191)(b)
|
||||
}
|
||||
|
||||
pub(crate) fn is_rtcp(buf: &[u8]) -> bool {
|
||||
// Not long enough to determine RTP/RTCP
|
||||
if buf.len() < 4 {
|
||||
return false;
|
||||
}
|
||||
|
||||
let rtcp_packet_type = buf[1];
|
||||
(192..=223).contains(&rtcp_packet_type)
|
||||
}
|
||||
|
||||
/// match_srtp is a MatchFunc that only matches SRTP and not SRTCP
|
||||
pub fn match_srtp(buf: &[u8]) -> bool {
|
||||
match_srtp_or_srtcp(buf) && !is_rtcp(buf)
|
||||
}
|
||||
|
||||
/// match_srtcp is a MatchFunc that only matches SRTCP and not SRTP
|
||||
pub fn match_srtcp(buf: &[u8]) -> bool {
|
||||
match_srtp_or_srtcp(buf) && is_rtcp(buf)
|
||||
}
|
||||
Vendored
+137
@@ -0,0 +1,137 @@
|
||||
use std::io;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use util::conn::conn_pipe::pipe;
|
||||
|
||||
use super::*;
|
||||
use crate::mux::mux_func::{match_all, match_srtp};
|
||||
|
||||
const TEST_PIPE_BUFFER_SIZE: usize = 8192;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_no_endpoints() -> crate::error::Result<()> {
|
||||
// In memory pipe
|
||||
let (ca, _) = pipe();
|
||||
|
||||
let mut m = Mux::new(Config {
|
||||
conn: Arc::new(ca),
|
||||
buffer_size: TEST_PIPE_BUFFER_SIZE,
|
||||
});
|
||||
|
||||
Mux::dispatch(&[0], &m.endpoints).await?;
|
||||
m.close().await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct MuxErrorConn {
|
||||
idx: AtomicUsize,
|
||||
data: Vec<Vec<u8>>,
|
||||
}
|
||||
|
||||
type Result<T> = std::result::Result<T, util::Error>;
|
||||
|
||||
#[async_trait]
|
||||
impl Conn for MuxErrorConn {
|
||||
async fn connect(&self, _addr: SocketAddr) -> Result<()> {
|
||||
Err(io::Error::new(io::ErrorKind::Other, "Not applicable").into())
|
||||
}
|
||||
|
||||
async fn recv(&self, buf: &mut [u8]) -> Result<usize> {
|
||||
let idx = self.idx.fetch_add(1, Ordering::SeqCst);
|
||||
if idx < self.data.len() {
|
||||
let n = std::cmp::min(buf.len(), self.data[idx].len());
|
||||
buf[..n].copy_from_slice(&self.data[idx][..n]);
|
||||
Ok(n)
|
||||
} else {
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("idx {} >= data.len {}", idx, self.data.len()),
|
||||
)
|
||||
.into())
|
||||
}
|
||||
}
|
||||
|
||||
async fn recv_from(&self, _buf: &mut [u8]) -> Result<(usize, SocketAddr)> {
|
||||
Err(io::Error::new(io::ErrorKind::Other, "Not applicable").into())
|
||||
}
|
||||
|
||||
async fn send(&self, _buf: &[u8]) -> Result<usize> {
|
||||
Err(io::Error::new(io::ErrorKind::Other, "Not applicable").into())
|
||||
}
|
||||
|
||||
async fn send_to(&self, _buf: &[u8], _target: SocketAddr) -> Result<usize> {
|
||||
Err(io::Error::new(io::ErrorKind::Other, "Not applicable").into())
|
||||
}
|
||||
|
||||
fn local_addr(&self) -> Result<SocketAddr> {
|
||||
Err(io::Error::new(io::ErrorKind::Other, "Not applicable").into())
|
||||
}
|
||||
|
||||
fn remote_addr(&self) -> Option<SocketAddr> {
|
||||
None
|
||||
}
|
||||
|
||||
async fn close(&self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_non_fatal_read() -> Result<()> {
|
||||
let expected_data = b"expected_data".to_vec();
|
||||
|
||||
let conn = Arc::new(MuxErrorConn {
|
||||
idx: AtomicUsize::new(0),
|
||||
data: vec![
|
||||
expected_data.clone(),
|
||||
expected_data.clone(),
|
||||
expected_data.clone(),
|
||||
],
|
||||
});
|
||||
|
||||
let mut m = Mux::new(Config {
|
||||
conn,
|
||||
buffer_size: TEST_PIPE_BUFFER_SIZE,
|
||||
});
|
||||
|
||||
let e = m.new_endpoint(Box::new(match_all)).await;
|
||||
let mut buff = vec![0u8; TEST_PIPE_BUFFER_SIZE];
|
||||
|
||||
let n = e.recv(&mut buff).await?;
|
||||
assert_eq!(&buff[..n], expected_data);
|
||||
|
||||
let n = e.recv(&mut buff).await?;
|
||||
assert_eq!(&buff[..n], expected_data);
|
||||
|
||||
let n = e.recv(&mut buff).await?;
|
||||
assert_eq!(&buff[..n], expected_data);
|
||||
|
||||
m.close().await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_non_fatal_dispatch() -> Result<()> {
|
||||
let (ca, cb) = pipe();
|
||||
|
||||
let mut m = Mux::new(Config {
|
||||
conn: Arc::new(ca),
|
||||
buffer_size: TEST_PIPE_BUFFER_SIZE,
|
||||
});
|
||||
|
||||
let e = m.new_endpoint(Box::new(match_srtp)).await;
|
||||
e.buffer.set_limit_size(1).await;
|
||||
|
||||
for _ in 0..25 {
|
||||
let srtp_packet = [128, 1, 2, 3, 4].to_vec();
|
||||
cb.send(&srtp_packet).await?;
|
||||
}
|
||||
|
||||
m.close().await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
+295
@@ -0,0 +1,295 @@
|
||||
use std::ops::Add;
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use dtls::crypto::{CryptoPrivateKey, CryptoPrivateKeyKind};
|
||||
use rcgen::{CertificateParams, KeyPair};
|
||||
use ring::rand::SystemRandom;
|
||||
use ring::rsa;
|
||||
use ring::signature::{EcdsaKeyPair, Ed25519KeyPair};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::dtls_transport::dtls_fingerprint::RTCDtlsFingerprint;
|
||||
use crate::error::{Error, Result};
|
||||
use crate::peer_connection::math_rand_alpha;
|
||||
use crate::stats::stats_collector::StatsCollector;
|
||||
use crate::stats::{CertificateStats, StatsReportType};
|
||||
|
||||
/// Certificate represents a X.509 certificate used to authenticate WebRTC communications.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RTCCertificate {
|
||||
/// DTLS certificate.
|
||||
pub(crate) dtls_certificate: dtls::crypto::Certificate,
|
||||
/// Timestamp after which this certificate is no longer valid.
|
||||
pub(crate) expires: SystemTime,
|
||||
/// Certificate's ID used for statistics.
|
||||
///
|
||||
/// Example: "certificate-1667202302853538793"
|
||||
///
|
||||
/// See [`CertificateStats`].
|
||||
pub(crate) stats_id: String,
|
||||
}
|
||||
|
||||
impl PartialEq for RTCCertificate {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.dtls_certificate == other.dtls_certificate
|
||||
}
|
||||
}
|
||||
|
||||
impl RTCCertificate {
|
||||
/// Generates a new certificate from the given parameters.
|
||||
///
|
||||
/// See [`rcgen::Certificate::from_params`].
|
||||
pub fn from_params(params: CertificateParams) -> Result<Self> {
|
||||
let not_after = params.not_after;
|
||||
let x509_cert = rcgen::Certificate::from_params(params)?;
|
||||
|
||||
let key_pair = x509_cert.get_key_pair();
|
||||
let serialized_der = key_pair.serialize_der();
|
||||
|
||||
let private_key = if key_pair.is_compatible(&rcgen::PKCS_ED25519) {
|
||||
CryptoPrivateKey {
|
||||
kind: CryptoPrivateKeyKind::Ed25519(
|
||||
Ed25519KeyPair::from_pkcs8(&serialized_der)
|
||||
.map_err(|e| Error::new(e.to_string()))?,
|
||||
),
|
||||
serialized_der,
|
||||
}
|
||||
} else if key_pair.is_compatible(&rcgen::PKCS_ECDSA_P256_SHA256) {
|
||||
CryptoPrivateKey {
|
||||
kind: CryptoPrivateKeyKind::Ecdsa256(
|
||||
EcdsaKeyPair::from_pkcs8(
|
||||
&ring::signature::ECDSA_P256_SHA256_ASN1_SIGNING,
|
||||
&serialized_der,
|
||||
&SystemRandom::new(),
|
||||
)
|
||||
.map_err(|e| Error::new(e.to_string()))?,
|
||||
),
|
||||
serialized_der,
|
||||
}
|
||||
} else if key_pair.is_compatible(&rcgen::PKCS_RSA_SHA256) {
|
||||
CryptoPrivateKey {
|
||||
kind: CryptoPrivateKeyKind::Rsa256(
|
||||
rsa::KeyPair::from_pkcs8(&serialized_der)
|
||||
.map_err(|e| Error::new(e.to_string()))?,
|
||||
),
|
||||
serialized_der,
|
||||
}
|
||||
} else {
|
||||
return Err(Error::new("Unsupported key_pair".to_owned()));
|
||||
};
|
||||
|
||||
let expires = if cfg!(target_arch = "arm") {
|
||||
// Workaround for issue overflow when adding duration to instant on armv7
|
||||
// https://github.com/webrtc-rs/examples/issues/5 https://github.com/chronotope/chrono/issues/343
|
||||
SystemTime::now().add(Duration::from_secs(172800)) //60*60*48 or 2 days
|
||||
} else {
|
||||
not_after.into()
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
dtls_certificate: dtls::crypto::Certificate {
|
||||
certificate: vec![rustls::Certificate(x509_cert.serialize_der()?)],
|
||||
private_key,
|
||||
},
|
||||
expires,
|
||||
stats_id: gen_stats_id(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Generates a new certificate with default [`CertificateParams`] using the given keypair.
|
||||
pub fn from_key_pair(key_pair: KeyPair) -> Result<Self> {
|
||||
let mut params = CertificateParams::new(vec![math_rand_alpha(16)]);
|
||||
|
||||
if key_pair.is_compatible(&rcgen::PKCS_ED25519) {
|
||||
params.alg = &rcgen::PKCS_ED25519;
|
||||
} else if key_pair.is_compatible(&rcgen::PKCS_ECDSA_P256_SHA256) {
|
||||
params.alg = &rcgen::PKCS_ECDSA_P256_SHA256;
|
||||
} else if key_pair.is_compatible(&rcgen::PKCS_RSA_SHA256) {
|
||||
params.alg = &rcgen::PKCS_RSA_SHA256;
|
||||
} else {
|
||||
return Err(Error::new("Unsupported key_pair".to_owned()));
|
||||
};
|
||||
params.key_pair = Some(key_pair);
|
||||
|
||||
RTCCertificate::from_params(params)
|
||||
}
|
||||
|
||||
/// Parses a certificate from the ASCII PEM format.
|
||||
#[cfg(feature = "pem")]
|
||||
pub fn from_pem(pem_str: &str) -> Result<Self> {
|
||||
let mut pem_blocks = pem_str.split("\n\n");
|
||||
let first_block = if let Some(b) = pem_blocks.next() {
|
||||
b
|
||||
} else {
|
||||
return Err(Error::InvalidPEM("empty PEM".into()));
|
||||
};
|
||||
let expires_pem =
|
||||
pem::parse(first_block).map_err(|e| Error::new(format!("can't parse PEM: {e}")))?;
|
||||
if expires_pem.tag() != "EXPIRES" {
|
||||
return Err(Error::InvalidPEM(format!(
|
||||
"invalid tag (expected: 'EXPIRES', got '{}')",
|
||||
expires_pem.tag()
|
||||
)));
|
||||
}
|
||||
let mut bytes = [0u8; 8];
|
||||
bytes.copy_from_slice(&expires_pem.contents()[..8]);
|
||||
let expires = if let Some(e) =
|
||||
SystemTime::UNIX_EPOCH.checked_add(Duration::from_secs(u64::from_le_bytes(bytes)))
|
||||
{
|
||||
e
|
||||
} else {
|
||||
return Err(Error::InvalidPEM("failed to calculate SystemTime".into()));
|
||||
};
|
||||
let dtls_certificate =
|
||||
dtls::crypto::Certificate::from_pem(&pem_blocks.collect::<Vec<&str>>().join("\n\n"))?;
|
||||
Ok(RTCCertificate::from_existing(dtls_certificate, expires))
|
||||
}
|
||||
|
||||
/// Builds a [`RTCCertificate`] using the existing DTLS certificate.
|
||||
///
|
||||
/// Use this method when you have a persistent certificate (i.e. you don't want to generate a
|
||||
/// new one for each DTLS connection).
|
||||
///
|
||||
/// NOTE: ID used for statistics will be different as it's neither derived from the given
|
||||
/// certificate nor persisted along it when using [`serialize_pem`].
|
||||
pub fn from_existing(dtls_certificate: dtls::crypto::Certificate, expires: SystemTime) -> Self {
|
||||
Self {
|
||||
dtls_certificate,
|
||||
expires,
|
||||
// TODO: figure out if it needs to be persisted
|
||||
stats_id: gen_stats_id(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Serializes the certificate (including the private key) in PKCS#8 format in PEM.
|
||||
#[cfg(feature = "pem")]
|
||||
pub fn serialize_pem(&self) -> String {
|
||||
// Encode `expires` as a PEM block.
|
||||
//
|
||||
// TODO: serialize as nanos when https://github.com/rust-lang/rust/issues/103332 is fixed.
|
||||
let expires_pem = pem::Pem::new(
|
||||
"EXPIRES".to_string(),
|
||||
self.expires
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.expect("expires to be valid")
|
||||
.as_secs()
|
||||
.to_le_bytes()
|
||||
.to_vec(),
|
||||
);
|
||||
format!(
|
||||
"{}\n{}",
|
||||
pem::encode(&expires_pem),
|
||||
self.dtls_certificate.serialize_pem()
|
||||
)
|
||||
}
|
||||
|
||||
/// get_fingerprints returns a SHA-256 fingerprint of this certificate.
|
||||
///
|
||||
/// TODO: return a fingerprint computed with the digest algorithm used in the certificate
|
||||
/// signature.
|
||||
pub fn get_fingerprints(&self) -> Vec<RTCDtlsFingerprint> {
|
||||
let mut fingerprints = Vec::new();
|
||||
|
||||
for c in &self.dtls_certificate.certificate {
|
||||
let mut h = Sha256::new();
|
||||
h.update(c.as_ref());
|
||||
let hashed = h.finalize();
|
||||
let values: Vec<String> = hashed.iter().map(|x| format! {"{x:02x}"}).collect();
|
||||
|
||||
fingerprints.push(RTCDtlsFingerprint {
|
||||
algorithm: "sha-256".to_owned(),
|
||||
value: values.join(":"),
|
||||
});
|
||||
}
|
||||
|
||||
fingerprints
|
||||
}
|
||||
|
||||
pub(crate) async fn collect_stats(&self, collector: &StatsCollector) {
|
||||
if let Some(fingerprint) = self.get_fingerprints().into_iter().next() {
|
||||
let stats = CertificateStats::new(self, fingerprint);
|
||||
collector.insert(
|
||||
self.stats_id.clone(),
|
||||
StatsReportType::CertificateStats(stats),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn gen_stats_id() -> String {
|
||||
format!(
|
||||
"certificate-{}",
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos() as u64
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_generate_certificate_rsa() -> Result<()> {
|
||||
let key_pair = KeyPair::generate(&rcgen::PKCS_RSA_SHA256);
|
||||
assert!(key_pair.is_err(), "RcgenError::KeyGenerationUnavailable");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_certificate_ecdsa() -> Result<()> {
|
||||
let kp = KeyPair::generate(&rcgen::PKCS_ECDSA_P256_SHA256)?;
|
||||
let _cert = RTCCertificate::from_key_pair(kp)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_certificate_eddsa() -> Result<()> {
|
||||
let kp = KeyPair::generate(&rcgen::PKCS_ED25519)?;
|
||||
let _cert = RTCCertificate::from_key_pair(kp)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_certificate_equal() -> Result<()> {
|
||||
let kp1 = KeyPair::generate(&rcgen::PKCS_ECDSA_P256_SHA256)?;
|
||||
let cert1 = RTCCertificate::from_key_pair(kp1)?;
|
||||
|
||||
let kp2 = KeyPair::generate(&rcgen::PKCS_ECDSA_P256_SHA256)?;
|
||||
let cert2 = RTCCertificate::from_key_pair(kp2)?;
|
||||
|
||||
assert_ne!(cert1, cert2);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_certificate_expires_and_stats_id() -> Result<()> {
|
||||
let kp = KeyPair::generate(&rcgen::PKCS_ECDSA_P256_SHA256)?;
|
||||
let cert = RTCCertificate::from_key_pair(kp)?;
|
||||
|
||||
let now = SystemTime::now();
|
||||
assert!(cert.expires.duration_since(now).is_ok());
|
||||
assert!(cert.stats_id.contains("certificate"));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "pem")]
|
||||
#[test]
|
||||
fn test_certificate_serialize_pem_and_from_pem() -> Result<()> {
|
||||
let kp = KeyPair::generate(&rcgen::PKCS_ECDSA_P256_SHA256)?;
|
||||
let cert = RTCCertificate::from_key_pair(kp)?;
|
||||
|
||||
let pem = cert.serialize_pem();
|
||||
let loaded_cert = RTCCertificate::from_pem(&pem)?;
|
||||
|
||||
assert_eq!(loaded_cert, cert);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
use crate::ice_transport::ice_server::RTCIceServer;
|
||||
use crate::peer_connection::certificate::RTCCertificate;
|
||||
use crate::peer_connection::policy::bundle_policy::RTCBundlePolicy;
|
||||
use crate::peer_connection::policy::ice_transport_policy::RTCIceTransportPolicy;
|
||||
use crate::peer_connection::policy::rtcp_mux_policy::RTCRtcpMuxPolicy;
|
||||
|
||||
/// A Configuration defines how peer-to-peer communication via PeerConnection
|
||||
/// is established or re-established.
|
||||
/// Configurations may be set up once and reused across multiple connections.
|
||||
/// Configurations are treated as readonly. As long as they are unmodified,
|
||||
/// they are safe for concurrent use.
|
||||
#[derive(Default, Clone)]
|
||||
pub struct RTCConfiguration {
|
||||
/// iceservers defines a slice describing servers available to be used by
|
||||
/// ICE, such as STUN and TURN servers.
|
||||
pub ice_servers: Vec<RTCIceServer>,
|
||||
|
||||
/// icetransport_policy indicates which candidates the ICEAgent is allowed
|
||||
/// to use.
|
||||
pub ice_transport_policy: RTCIceTransportPolicy,
|
||||
|
||||
/// bundle_policy indicates which media-bundling policy to use when gathering
|
||||
/// ICE candidates.
|
||||
pub bundle_policy: RTCBundlePolicy,
|
||||
|
||||
/// rtcp_mux_policy indicates which rtcp-mux policy to use when gathering ICE
|
||||
/// candidates.
|
||||
pub rtcp_mux_policy: RTCRtcpMuxPolicy,
|
||||
|
||||
/// peer_identity sets the target peer identity for the PeerConnection.
|
||||
/// The PeerConnection will not establish a connection to a remote peer
|
||||
/// unless it can be successfully authenticated with the provided name.
|
||||
pub peer_identity: String,
|
||||
|
||||
/// Certificates describes a set of certificates that the PeerConnection
|
||||
/// uses to authenticate. Valid values for this parameter are created
|
||||
/// through calls to the generate_certificate function. Although any given
|
||||
/// DTLS connection will use only one certificate, this attribute allows the
|
||||
/// caller to provide multiple certificates that support different
|
||||
/// algorithms. The final certificate will be selected based on the DTLS
|
||||
/// handshake, which establishes which certificates are allowed. The
|
||||
/// PeerConnection implementation selects which of the certificates is
|
||||
/// used for a given connection; how certificates are selected is outside
|
||||
/// the scope of this specification. If this value is absent, then a default
|
||||
/// set of certificates is generated for each PeerConnection instance.
|
||||
pub certificates: Vec<RTCCertificate>,
|
||||
|
||||
/// icecandidate_pool_size describes the size of the prefetched ICE pool.
|
||||
pub ice_candidate_pool_size: u8,
|
||||
}
|
||||
|
||||
impl RTCConfiguration {
|
||||
/// get_iceservers side-steps the strict parsing mode of the ice package
|
||||
/// (as defined in https://tools.ietf.org/html/rfc7064) by copying and then
|
||||
/// stripping any erroneous queries from "stun(s):" URLs before parsing.
|
||||
pub(crate) fn get_ice_servers(&self) -> Vec<RTCIceServer> {
|
||||
let mut ice_servers = self.ice_servers.clone();
|
||||
|
||||
for ice_server in &mut ice_servers {
|
||||
for raw_url in &mut ice_server.urls {
|
||||
if raw_url.starts_with("stun") {
|
||||
// strip the query from "stun(s):" if present
|
||||
let parts: Vec<&str> = raw_url.split('?').collect();
|
||||
*raw_url = parts[0].to_owned();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ice_servers
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_configuration_get_iceservers() {
|
||||
{
|
||||
let expected_server_str = "stun:stun.l.google.com:19302";
|
||||
let cfg = RTCConfiguration {
|
||||
ice_servers: vec![RTCIceServer {
|
||||
urls: vec![expected_server_str.to_owned()],
|
||||
..Default::default()
|
||||
}],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let parsed_urls = cfg.get_ice_servers();
|
||||
assert_eq!(parsed_urls[0].urls[0], expected_server_str);
|
||||
}
|
||||
|
||||
{
|
||||
// ignore the fact that stun URLs shouldn't have a query
|
||||
let server_str = "stun:global.stun.twilio.com:3478?transport=udp";
|
||||
let expected_server_str = "stun:global.stun.twilio.com:3478";
|
||||
let cfg = RTCConfiguration {
|
||||
ice_servers: vec![RTCIceServer {
|
||||
urls: vec![server_str.to_owned()],
|
||||
..Default::default()
|
||||
}],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let parsed_urls = cfg.get_ice_servers();
|
||||
assert_eq!(parsed_urls[0].urls[0], expected_server_str);
|
||||
}
|
||||
}
|
||||
|
||||
/*TODO:#[test] fn test_configuration_json() {
|
||||
|
||||
let j = r#"
|
||||
{
|
||||
"iceServers": [{"URLs": ["turn:turn.example.org"],
|
||||
"username": "jch",
|
||||
"credential": "topsecret"
|
||||
}],
|
||||
"iceTransportPolicy": "relay",
|
||||
"bundlePolicy": "balanced",
|
||||
"rtcpMuxPolicy": "require"
|
||||
}"#;
|
||||
|
||||
conf := Configuration{
|
||||
ICEServers: []ICEServer{
|
||||
{
|
||||
URLs: []string{"turn:turn.example.org"},
|
||||
Username: "jch",
|
||||
Credential: "topsecret",
|
||||
},
|
||||
},
|
||||
ICETransportPolicy: ICETransportPolicyRelay,
|
||||
BundlePolicy: BundlePolicyBalanced,
|
||||
RTCPMuxPolicy: RTCPMuxPolicyRequire,
|
||||
}
|
||||
|
||||
var conf2 Configuration
|
||||
assert.NoError(t, json.Unmarshal([]byte(j), &conf2))
|
||||
assert.Equal(t, conf, conf2)
|
||||
|
||||
j2, err := json.Marshal(conf2)
|
||||
assert.NoError(t, err)
|
||||
|
||||
var conf3 Configuration
|
||||
assert.NoError(t, json.Unmarshal(j2, &conf3))
|
||||
assert.Equal(t, conf2, conf3)
|
||||
}*/
|
||||
}
|
||||
+2108
File diff suppressed because it is too large.
Load diff
Loaded 100 of 143 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user