Skip to content

web: share the desktop with a browser on the LAN - #5

Open
pdev-labs wants to merge 10 commits into
customfrom
web-access
Open

pdev-labs wants to merge 10 commits into
customfrom
web-access

Conversation

@pdev-labs

Copy link
Copy Markdown
Owner

What this adds

lfa web start|status|stop|doctor shares the running desktop (:1) with another device on the same Wi-Fi. The viewer opens the URL, enters one 8-character passcode, and gets video plus mouse and keyboard control.

How it works

  • ffmpeg captures :1 with x11grab/xcbgrab and encodes H.264 (h264_mediacodec when available, libx264 -preset ultrafast -tune zerolatency otherwise).
  • Annex-B NAL units go over an authenticated WebSocket; the browser decodes them with WebCodecs and draws to a canvas.
  • Pointer, wheel, and keyboard events return on the same socket and are injected with XTEST through python-xlib.
  • The passcode is 8 characters from /dev/urandom (uppercase letters and digits, no 0/O/1/I), kept in memory, compared with hmac.compare_digest, and never written to disk.
  • The server binds to the Wi-Fi IPv4 address, serves a self-signed HTTPS page by default, and limits failed logins to five per minute per address.
  • --transport webrtc keeps a go2rtc path. It is not the default because Android 16 denies the netlink route query pion uses to enumerate interfaces.

--size captures the full display by default and scales to the requested output size. --view-only, --fps, --encoder, --http, --detach, and --bind are available.

Tests

  • tests/test-all.sh: PASS=1022 FAIL=0 (README assertions updated).
  • shellcheck -S warning: clean on lib/lfa-web.sh, install_linux.sh, and tests/test-all.sh.
  • On-device (Vivo V2437, Android 16): lfa web doctor all green; login, player page, H.264 config (avc1.640020, 1280x1024), 30 fps frames, and XTEST pointer moves to the expected pixels.
  • Local: login redirects, bad/good passcode, WebSocket auth accept/reject, and page rendering.

Notes

  • The generated start-linux scripts now run pd_login in a subshell instead of under nohup, which failed because pd_login is a shell function.
  • go2rtc is pinned to v1.9.15 and checked with SHA-256; the arm64 checksum matches the downloaded binary.

Add `lfa web start|status|stop|doctor`. The share captures display :1,
encodes H.264 with h264_mediacodec when available, and streams Annex-B
NAL units over an authenticated WebSocket. The browser decodes them with
WebCodecs and sends pointer and keyboard events back over the same
socket, which the server injects with XTEST.

One 8-character passcode from /dev/urandom protects the page. It stays
in memory and is compared with hmac.compare_digest. The server binds to
the Wi-Fi address, serves a self-signed HTTPS page by default, and limits
failed logins. `--transport webrtc` keeps a go2rtc path for hosts where
pion can enumerate interfaces; Android 16 denies the netlink query, so
WebCodecs is the default.

The share stops from `lfa web stop`, `lfa stop`, stop-linux, and
uninstall. Also run the generated pd_login sessions in a subshell so
`nohup` no longer fails on a shell function.
A killed X server can leave $PREFIX/tmp/.X11-unix/X1 behind. The old
check accepted the socket and started ffmpeg against a dead display.
Probe with xdpyinfo or python-xlib first and fall back to the socket only
when neither tool is available.
Termux:X11 resizes its virtual screen when the Android window changes.
The share read the size once at startup, so a viewer that connected after
a resize made ffmpeg fail with "Capture area ... outside the screen
size", which the player showed as a reconnect loop. Run ffmpeg without
-video_size so x11grab reads the current screen, and let the canvas take
its size from the decoded frames. --size now scales the output instead of
cropping the capture.
Reconnect the XTEST display when the desktop is restarted after the
share starts, and read the root geometry for each pointer move so a
resized screen still maps to the right pixels. Round the captured size to
even dimensions so h264_mediacodec accepts screens with an odd height.
Blur the player buttons after a click so they do not keep keyboard focus.
When VideoDecoder is missing, the player asks for ?mode=mjpeg on the
same authenticated WebSocket that carries input. The server runs ffmpeg's
mjpeg encoder, splits complete JPEG images, and sends them to the
browser. The player draws them through an <img> element, so any browser
can view the desktop. It uses more bandwidth than the H.264 path.
The MJPEG fallback draws each frame from an <img> with a blob: URL.
img-src did not include blob:, so the browser blocked every frame and the
player stayed on "Waiting for video". Add blob: to img-src.
Touch devices show a floating menu with a relative touchpad, tap-to-
position, mouse buttons, hold-drag, scrolling, arrows, and modifier
latches. A text field opens the on-screen keyboard and translates its
input into key events. Add the relative `mr` pointer event on the server.
Avoid a blob URL and an <img> load per frame. createImageBitmap decodes
off the main thread, which helps the fallback keep up at 60 fps.
The canvas is now sized explicitly instead of relying on object-fit, so
the desktop keeps its aspect ratio by default. The touch panel adds Fit,
Stretch, Fill, and 1:1 modes plus a 50-200% zoom control, and remembers
the choice in localStorage. Input mapping uses the displayed element
rect, so it follows every mode.
Add `lfa codec`, a bridge between VA-API in the container and the
phone's MediaCodec decoders. A Termux daemon drives AMediaCodec over a
Unix socket in the shared /tmp, and a standalone libva driver in the
guest turns decode calls into that protocol and copies NV12 frames into
VA surfaces.

Commands: install, status, start, stop, doctor, test, play, env, run.
The daemon follows the desktop through start-linux/stop-linux, and
existing launchers are patched on install. The driver installs under
/usr/local/lib/lfa-codec/dri and does not touch the distro's Mesa files.

H.264 (Baseline/Main/High), HEVC Main, VP9 Profile 0, and VP8 are
supported. This is the CPU copy path, so Chromium and Firefox stay on
software decode; mpv, VLC, and ffmpeg use the bridge.

Vendored sources: the daemon from lfdevs/termux-va (GPL-3.0) and the
driver from Re-s/droidspaces-media-decode (Apache-2.0).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant