Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ tag names, define the order. See the table at the bottom.

### Added

- Hardware video decode. `lfa codec` installs a Termux MediaCodec daemon and
a VA-API driver in the container, so mpv, VLC, and ffmpeg decode H.264,
HEVC, VP9, and VP8 on the phone's decoder instead of the CPU. `install`,
`status`, `start`, `stop`, `doctor`, `test`, `play`, `env`, and `run` cover
setup and day-to-day use. The bridge copies frames through the CPU and does
not export dmabufs, so Chromium and Firefox still use software decode. The
daemon runs over a Unix socket in the shared `/tmp` and stops with
`lfa stop`.
- Browser access to the desktop. `lfa web start` shares display `:1` with
another device on the local network, with `status`, `stop`, and `doctor`
alongside it. The command prints one 8-character passcode made of letters
and digits; every viewer who enters it gets mouse and keyboard control until
the share stops. `--view-only`, `--size`, `--fps`, `--encoder`, `--http`,
`--detach`, and `--transport` adjust the share. The default transport sends
H.264 over a WebSocket and decodes it with WebCodecs in the browser, which
works on Android. A browser without WebCodecs gets an MJPEG fallback.
Phone and tablet viewers get a floating touch panel with a relative
touchpad, tap-to-position, mouse buttons, hold-drag, scrolling, and an
on-screen keyboard. The panel also has Fit, Stretch, Fill, and 1:1 screen
modes with zoom. `--transport webrtc` uses go2rtc v1.9.15, downloaded on
first use after a SHA-256 check. Video uses `h264_mediacodec` when ffmpeg
supports it and falls back to `libx264 -preset ultrafast -tune zerolatency`.
The passcode stays in memory, and the only LAN listener is the Python
server, which serves a self-signed HTTPS page by default.
- Separate Android and external storage policies. Guided setup now offers
Both, Android only, External only, or None instead of one storage switch.
`lfa install` accepts `--android-storage on|off` and
Expand Down
153 changes: 148 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ hardware, not simulated. Full details, root causes, and evidence are in
| Alpine 3.18 (Android 5) | Yes | Yes | legacy VNC desktop, panel works; X11 and Both hidden | v1.7.1 |
| Ubuntu GNOME 46 (experimental) | Yes | Yes | shell and apps run; session services missing | v1.7.0 |

> The test suite runs `965` checks with 0 failures. The same suite runs on the
> The test suite runs `1057` checks with 0 failures. The same suite runs on the
> devices (`tests/device-matrix.sh`). The table is as of v1.7.1. Ubuntu was
> re-verified on v1.7.1, and Alpine 3.18 was re-verified on Android 5 on
> 2026-11-04; the other rows were last verified on v1.7.0. A row is
Expand All @@ -152,6 +152,7 @@ same session:
| Alpine 3.18, XFCE, and VNC on the Oppo A37fw (2016) | 3m 32s, 204 MB |
| Ubuntu 24.04 and GNOME 46 on the Vivo V2437 | 27m 45s, about 2.5 GB |
| Desktop cold start (XFCE, X11 on the Vivo) | about 10 seconds after `start-linux` |
| Hardware decode on the Vivo (MT6878) | 1080p H.264/HEVC/VP9 and 4K H.264 matched software output byte-for-byte; the copy path is CPU-bound |
| GNOME over software rendering | 1 to 7 FPS video playback, usable shell |

## Hard problems solved
Expand Down Expand Up @@ -481,14 +482,16 @@ is accepted.
| `lfa appimage` (`app`, `ai`) | extract an AppImage once and add a menu entry |
| `lfa run` | run an AppImage once without installing it |
| `lfa storage` | Android storage status, repair, run, protected data helpers, and separate Android/external access toggles |
| `lfa web` | share the running desktop with a browser on the local network; `start`, `status`, `stop`, `doctor` |
| `lfa codec` | hardware video decode through Android MediaCodec; `install`, `status`, `start`, `stop`, `doctor`, `test`, `play`, `env`, `run` |
| `lfa diagnostics` (`diag`) | write a redacted support bundle |
| `lfa notice` | `on`, `off`, or `status` for the `start-linux` change notice |
| `lfa version` (`ver`) / `lfa help` | version and command list |
| `lfa check-update` | ask GitHub for a newer manager release; prints it and posts an Android notification through Termux:API |

Short commands exist only where they cannot be confused with another one:
`status`, `stop`, `usb`, `storage`, and `android` stay spelled out. The long
forms always work.
`status`, `stop`, `usb`, `storage`, `web`, and `android` stay spelled out. The
long forms always work.

Examples:

Expand All @@ -510,6 +513,10 @@ lfa usb exec 001/002 -- lfa-usb desc
lfa appimage add ~/Foo.AppImage
lfa appimage run Foo
lfa storage status
lfa web start -i ubuntu
lfa web status -i ubuntu
lfa codec install -i ubuntu
lfa codec play ~/Videos/sample.mp4
lfa notice off
```

Expand Down Expand Up @@ -677,6 +684,141 @@ only the binds you kept: Android shared storage at `/sdcard` and
disabled category is not reachable through `/storage`. Disabling both also
hides the other Android and Termux host paths that `--isolated` skips.

### Web desktop in a browser

`lfa web` shares the running desktop with another device on the same Wi-Fi.
The viewer opens a URL in a browser, types the passcode printed on the phone,
and gets the desktop with mouse and keyboard control.

```bash
lfa web start -i ubuntu
lfa web status -i ubuntu
lfa web stop -i ubuntu
```

`start` prints the URL and one 8-character passcode:

```text
URL: https://192.168.1.42:8765
Passcode: K7M2Q9XA
```

The code uses uppercase letters and digits, with 0/O and 1/I left out so it is
easier to read from a screen. It is generated per run, kept in memory, and
never written to the log, the state file, or the diagnostics bundle. Every
viewer that enters it gets full control until the share stops. Use
`--view-only` to send video without input.

The default transport sends H.264 over a WebSocket and the browser decodes it
with WebCodecs. This works on Android, where WebRTC's network discovery is
blocked by the platform. `--transport webrtc` uses go2rtc for a WebRTC stream
on devices where that path is available.

```bash
lfa web start -i ubuntu --size 1920x1080 --fps 30
lfa web start -i ubuntu --encoder x264
lfa web start -i ubuntu --view-only --http
lfa web start -i ubuntu --detach
lfa web start -i ubuntu --transport webrtc
```

`--encoder auto` (the default) picks `h264_mediacodec` when ffmpeg reports it
and falls back to `libx264 -preset ultrafast -tune zerolatency`. The default
captures the whole display at 30 fps. `--size WxH` scales the capture to that
output size, which is useful on a large desktop. The phone's hardware encoder
keeps CPU use low, and the browser decodes H.264 with its own hardware path
when it has one.

The MJPEG fallback keeps up at 60 fps when you ask for it. On the test phone's
hotspot, `--size 1280x720 --fps 60` used about 8 Mbps and
`--size 960x540 --fps 60` about 6 Mbps. The H.264 path uses much less
bandwidth for the same picture. The viewer HUD shows the actual frame rate and
bitrate, so you can tune `--size` and `--fps` against what the viewer can
decode.

WebCodecs gives the lowest latency and needs Chrome 94+, Edge 94+, or Safari
16.4+. Firefox support depends on the build. A browser without WebCodecs
automatically gets an MJPEG fallback, which works in every browser but uses
more bandwidth. Use a WebCodecs browser when you can.

A phone or tablet viewer shows a floating menu button. The touch panel
switches between a relative touchpad and tap-to-position pointer mode, and
adds left, right, and middle click, hold-drag, scrolling, arrow and modifier
keys, plus a text field that opens the on-screen keyboard. It also has Fit,
Stretch, Fill, and 1:1 screen modes with a zoom control. The screen mode is
remembered in the browser.

`lfa web doctor` checks ffmpeg's X11 capture input (`x11grab` or `xcbgrab`)
and H.264 encoders, the Python `websockets` and `python-xlib` modules, the
display, and the LAN address. It also reports go2rtc, which only the optional
WebRTC transport uses.

The server binds to the Wi-Fi IPv4 address, not to cellular or to every
interface. The passcode page runs over HTTPS with a self-signed certificate;
the browser shows a warning once. `--http` skips TLS when the network is
trusted. Five failed passcodes per minute per address are refused, and the
comparison uses `hmac.compare_digest`. The video WebSocket uses the same
session cookie as the page. In the WebRTC transport, go2rtc listens on
`127.0.0.1` only and the Python server proxies the WebRTC handshake.

`Ctrl-C`, `lfa web stop`, and `lfa stop` stop ffmpeg, go2rtc when it is in use,
and the server. A stale PID is never signalled unless its command line still
matches the server.

The share is for the local network and does not open a port on the internet.
If the browser connects but video stays black, check that the Wi-Fi access
point allows client-to-client traffic; guest networks often block it. Try
`--encoder x264` if the phone's hardware encoder fails on a large display.

### Hardware video decode

`lfa codec` routes VA-API decode from the container to Android's MediaCodec
hardware decoders. H.264 (Constrained Baseline, Main, High), HEVC Main, VP9
Profile 0, and VP8 are supported. The phone's decoder does the work; the
container gets NV12 frames through a small driver.

```bash
lfa codec install -i ubuntu
lfa codec status
lfa codec test
lfa codec play ~/Videos/sample.mp4
```

`install` builds a Termux-side daemon and a libva driver inside the container.
The daemon starts with the desktop and stops with `lfa stop`. `test` decodes a
small sample with the bridge and with software decode, then compares the two
results. `play` runs mpv with `--hwdec=vaapi-copy` and the right environment.
A Termux-local file is copied into the shared `/tmp` first; `/sdcard`,
`/storage`, and container paths are used as they are.

Use it with any VA-API consumer:

```bash
lfa codec run -- ffmpeg -hwaccel vaapi -vaapi_device :1 \
-hwaccel_output_format vaapi -i in.mp4 -f null -
lfa codec env
```

The container and Termux share `/tmp`, so the daemon socket is
`/tmp/termux-va/termux-va.sock` in the guest. The driver is installed under
`/usr/local/lib/lfa-codec/dri`, separate from the distro's Mesa files.

Two limits matter:

- The bridge copies each decoded frame through the CPU. At low resolutions
software decode can be just as fast; the gain is CPU headroom and larger
frames, not a guaranteed higher frame rate. The test phone decoded 1080p
H.264, HEVC, and VP9 byte-for-byte identically to software.
- Chromium and Firefox need a DRM render node and dmabuf export to use a
hardware decoder. The container normally cannot open `/dev/dri`, so those
browsers stay on software decode. mpv and ffmpeg use the copy path; VLC can
decode with `--avcodec-hw=vaapi`, though its GL output may still choose
software.

`lfa codec doctor` reports the daemon, socket, driver, libva, and whether a DRM
render node is available. If decode fails, check that the desktop is running
on `:1` and that `lfa codec status` shows the daemon as running.

### Android/data and Android/obb

Android 11 and later block `/storage/emulated/0/Android/data` and
Expand Down Expand Up @@ -1323,7 +1465,8 @@ short list is:
- `systemd`, `snapd`, Flatpak and bubblewrap sandboxes
- Docker, Podman, LXC/LXD, libvirt, VirtualBox, QEMU/KVM, Waydroid
- WireGuard/OpenVPN/TUN/TAP, raw packet capture, iptables/nftables enforcement
- CUDA, ROCm, vendor GPU compute, hardware video encode/decode
- CUDA, ROCm, vendor GPU compute, hardware video encode (decode is available
through `lfa codec`)
- kernel modules, real `mount`/`chroot`/`unshare`, real root/SELinux control
- Bluetooth/BlueZ, NFC, camera as `/dev/video*`, sensors, USB host as device
nodes (`lfa usb` provides userspace access instead)
Expand Down Expand Up @@ -1652,7 +1795,7 @@ plus `script` from util-linux for the pty test, which is skipped if absent:

```bash
chmod +x tests/test-all.sh
./tests/test-all.sh # expect: PASS=965 FAIL=0
./tests/test-all.sh # expect: PASS=1057 FAIL=0
```

The suite is deliberately stubbed. It cannot prove a desktop actually appears.
Expand Down
21 changes: 19 additions & 2 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ In scope:

- command injection or path traversal through installer inputs (distro name,
instance name, hostname, username, timezone)
- secret leakage: install passwords, the VNC password, or the sudo shim secret
ending up readable outside the intended mode-`0600` file
- secret leakage: install passwords, the VNC password, the `lfa web`
passcode, or the sudo shim secret ending up readable outside the intended
mode-`0600` file
- authentication bypass or passcode brute force against `lfa web`
- trusting a downloaded artifact from a host that should not be trusted
- anything that runs code as the Termux user without the user asking

Expand All @@ -66,3 +68,18 @@ Out of scope:
Code repository is added.
- Scraped download URLs are restricted to the vendor's own hosts.
- `:latest` container images can be pinned with `LFA_IMAGE_<DISTRO>`.
- The browser share (`lfa web`) generates one 8-character passcode per run
from `/dev/urandom`, keeps it in memory, and passes it to the server on
stdin. It is not written to the log, state file, shell history, or
diagnostics bundle. Failed logins are limited to five per minute per
address and compared with `hmac.compare_digest`. The server binds to the
Wi-Fi IPv4 address, not cellular. The default WebCodecs transport needs no
other listener; when `--transport webrtc` is used, go2rtc listens on
`127.0.0.1` only. The passcode page uses a self-signed HTTPS certificate by
default.
- The hardware decode daemon (`lfa codec`) listens on a Unix socket under
Termux's private `$TMPDIR`, not on TCP. The daemon runs as the Termux user
and needs no root; the driver is built from vendored source in the
container. The socket is reachable by the Termux app and by the guest that
shares `/tmp`, which is the intended boundary. The daemon is stopped by
`lfa stop` and by uninstalling the instance.
87 changes: 87 additions & 0 deletions codec/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Hardware video decode

This directory contains the two halves of the MediaCodec VA-API bridge:

- `daemon/` runs in Termux and drives Android's `MediaCodec` decoders through
the NDK. It listens on a Unix socket under `$TMPDIR/termux-va/`.
- `vaapi/` is a libva driver loaded inside the Linux container. It turns VA-API
decode calls into the daemon protocol and copies the decoded NV12 frames into
VA surfaces.

The container shares Termux's temporary directory (`proot-distro --shared-tmp`
does this), so the daemon socket is visible as
`/tmp/termux-va/termux-va.sock` inside the guest.

```
mpv / ffmpeg / VLC
|
libva
|
lfa_codec_drv_video.so (container)
|
/tmp/termux-va/termux-va.sock (shared tmp)
|
termux-va daemon (Termux)
|
Android MediaCodec
```

## What works

Decode is implemented for H.264 (Constrained Baseline, Main, High), HEVC Main,
VP9 Profile 0, and VP8. Output is NV12. The driver also implements
`vaDeriveImage`, `vaCreateImage`, `vaGetImage`, and `vaMapBuffer`, which is the
path mpv and ffmpeg use for `vaapi-copy` / `hwdownload`.

The bridge does not provide a DRM render node or zero-copy dmabuf export. On
devices where the container cannot open `/dev/dri`, `vaExportSurfaceHandle`
cannot hand a surface to a GPU consumer. That means Chromium and Firefox still
fall back to software decode. Media players using the copy path are the
supported target.

## Build

The daemon is built by Termux's clang:

```sh
make -C daemon
```

The driver is built inside the container with `libva-dev`, `libdrm-dev`,
`build-essential`, `pkg-config`, and `python3`:

```sh
make -C vaapi gen # regenerate the vtable for the container's libva
make -C vaapi
make -C vaapi install # /usr/local/lib/lfa-codec/dri/lfa_codec_drv_video.so
```

`lfa codec install` runs both steps for you.

## Runtime environment

The container-side consumer needs:

```sh
LIBVA_DRIVER_NAME=lfa_codec
LIBVA_DRIVERS_PATH=/usr/local/lib/lfa-codec/dri
DMD_ENDPOINT=unix:/tmp/termux-va/termux-va.sock
```

`lfa codec play FILE` sets these and starts mpv with `--hwdec=vaapi-copy`.

## Provenance and licenses

`daemon/termux-va.c` and `common/tva_protocol.h` come from
[lfdevs/termux-va](https://github.com/lfdevs/termux-va), which is GPL-3.0. The
files keep their upstream copyright and modification notices. LFA is also
GPL-3.0, so the combined work is distributed under the same license.

`vaapi/` comes from the socket-based VA-API proxy driver in
[Re-s/droidspaces-media-decode](https://github.com/Re-s/droidspaces-media-decode)
(commit before the V4L2 rewrite), which is Apache-2.0. The license text is in
`vaapi/LICENSE`. Apache-2.0 is compatible with GPL-3.0; the combined work is
distributed under GPL-3.0.

The LFA changes are limited to the build layout, the driver name and version
string, and the `lfa codec` integration.
Loading
Loading