Skip to content

webadmin: play H.265 in the browser with a WebAssembly fallback - #54

Merged
tridge merged 5 commits into
ArduPilot:mainfrom
tridge:pr-h265-view
Nov 30, 2026
Merged

tridge merged 5 commits into
ArduPilot:mainfrom
tridge:pr-h265-view

Conversation

@tridge

@tridge tridge commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Most desktop browsers on Linux, and Firefox generally, refuse HEVC in Media Source Extensions, so an H.265 video slot showed nothing in the web viewer beyond a pointer to ffplay. This follows the approach AP_CameraGimbal uses for its own live view.

How it works

  • The owner and share pages still try native playback (mpegts.js + MSE) first; Safari and Chrome/Edge with a hardware HEVC decoder keep playing natively.
  • When native playback rejects the stream (addSourceBuffer NotSupportedError, an unsupported-codec error, or element decode errors), the page falls back to a worker that reads the same MPEG-TS WebSocket, demuxes it, and decodes with a WebAssembly build of FFmpeg 8.1.3's HEVC decoder and parser (329 KB gzipped, fetched only when needed).
  • Frames are drawn into a canvas whose captureStream() feeds the page's existing <video>, so its controls, pause and Picture-in-Picture keep working across reconnects.
  • The worker reports a stream that is not HEVC, so a publisher switching to H.264 goes back to native playback. It recovers from loss at the next VPS/SPS/PPS or IRAP, skips ahead to a newer keyframe when it falls behind live, and accepts a lasting increase in network delay instead of skipping forever.
  • Both pages' players were reworked so stale callbacks from a superseded attempt are ignored, retries are serialised, and native/software switching is bounded.
  • The share page now uses mpegts.js wherever it is supported, which includes ManagedMediaSource on iOS 17.1+. It falls back to HLS when the video port cannot be reached, and tries the software decoder when Safari refuses HEVC in HLS.
  • Static asset URLs carry a content hash (static_v()), since static files are browser-cached for a day.

webadmin/hevc/build.sh reproduces the checked-in hevc-codec.js / hevc-codec.wasm.gz from the signed FFmpeg release with Emscripten 3.1.69. The LGPL text and source details are in webadmin/static/vendor/hevc/.

Testing

  • Node tests run the worker and the shipped WASM over real HEVC MPEG-TS fixtures. They cover exact decode order, chunk and PES boundaries, split PES headers, loss and discontinuity recovery, and lag handling on a continuous timeline against an independent clock.
  • A VM harness covers the share page's mode transitions.
  • An optional browser test (tests/test_hevc_browser.py, skipped unless Playwright and Chrome are available) drives both pages in headless Chrome against a live ffmpeg publisher switching HEVC → H.264 → HEVC, and checks that a user pause survives a publisher restart.
  • Full suite passes.
  • Tested on the FireVPS proxy with a live MT11 camera: the 1280×720 HEVC stream plays at about 20 fps in software. A 3840×2160 stream plays but is laggy at about 3 fps, because single-threaded WASM decoding cannot keep up with 4K.

Known limits

  • The software path is video only.
  • The publisher must send VPS/SPS/PPS before each keyframe (most encoders do; x265 needs repeat-headers=1).
  • Recorded-log playback (log_play.html) is not covered yet.

Nothing calls it, and its premise that browsers cannot play HEVC no
longer holds for the web viewer.
@tridge tridge added the AIReview Request an automated AI review; picked up by the reviewprs sweep label Nov 30, 2026
@tridge
tridge marked this pull request as draft November 30, 2026 10:23
@AP-Review

AP-Review commented Nov 30, 2026 •

Copy link
Copy Markdown

Deprecated — see below for the updated review.

Previous review (2026-11-30)

Automated review note — AI-generated (Claude+Codex), validated against the live diff. Please sanity-check before acting.
Verdict: COMMENT

Reviewed at head 6c26b5c3b6.
Full report: https://firmware.ardupilot.org/Tools/APReview/DevCallReviews/PRReviews/ardupilot/supportproxy/54/1.html#prSupportProxy-54

The HEVC software fallback looks solid and its tests pass, but one share-page recovery path can leave a viewer stuck.

Should fix

  • webadmin/static/watch.js:279: if MSE rejects HEVC and the publisher returns as H.264 before the software decoder sees an HEVC PMT, mseFailed is never cleared. MSE is then skipped, and without native HLS the page halts on "cannot play" until reloaded. Record which codec MSE rejected, or allow one fresh MSE attempt under the existing flip guard.
Minor (2)
  • webadmin/static/hevc-player.js:108: a worker script that fails to load twice before ready is reported as fatal, so a transient HTTP failure gives up permanently (owner page giveUp(), share page HLS or halt). Treat it as non-fatal like a decoder download failure.
  • webadmin/static/hevc-worker.js:290: stale duplicate of the comment on line 291; drop it.

Static files are browser-cached for a day, so a deploy could leave a
page running old scripts. static_v() appends a hash of the file so a
changed file gets a new URL.
FFmpeg 8.1.3's HEVC decoder and parser with libswscale, built with
Emscripten 3.1.69 for the web viewer's software fallback. The wrapper
takes Annex-B input in arbitrary chunks, splits access units with the
parser and returns RGBA frames with their presentation time. build.sh
reproduces the checked-in hevc-codec.js and hevc-codec.wasm.gz from
the signed FFmpeg release; the LGPL text and source details are beside
them.
Most desktop browsers on Linux, and Firefox generally, refuse HEVC in
Media Source Extensions, so an H.265 slot showed nothing but a pointer
to ffplay. When native playback rejects the stream, the owner and share
pages now fall back to a worker that reads the same MPEG-TS WebSocket,
demuxes it and decodes it with the WASM decoder. Frames go through a
canvas captureStream() into the page's own <video>, so controls, pause
and Picture-in-Picture keep working across reconnects.

The worker reports a stream that is not HEVC so the page returns to
native playback, recovers from loss at the next VPS/SPS/PPS or IRAP,
and skips to a newer keyframe when it falls behind live while accepting
a lasting network delay. Each page's player is reworked so stale
callbacks from a superseded attempt are ignored, retries are serialised
and native/software switching is bounded.

The share page now uses mpegts.js whenever it is supported, which
includes ManagedMediaSource on iOS 17.1+, and falls back to HLS when the
video port cannot be reached.
Node tests run the worker over real HEVC MPEG-TS through the shipped
WASM: exact decode order, chunk and PES boundaries, split PES headers,
loss and discontinuity recovery, and lag handling on a continuous
timeline against an independent clock. A VM harness covers the share
page's mode transitions. An optional browser test drives the owner and
share pages in headless Chrome with HEVC MSE refused, against a live
ffmpeg publisher switching between HEVC and H.264.
@AP-Review

Copy link
Copy Markdown

Automated review note — AI-generated (Claude+Codex), validated against the live diff. Please sanity-check before acting.
Verdict: ACCEPT

Reviewed at head 8805140c6f.
Full report: https://firmware.ardupilot.org/Tools/APReview/DevCallReviews/PRReviews/ardupilot/supportproxy/54/2.html#prSupportProxy-54

No open issues at head 8805140: the whole diff was re-reviewed and the Node worker, controller and webadmin pytest suites pass.

All three findings from the review of 6c26b5c are fixed. The share page now gives MSE another try when a rejected HEVC stream returns as H.264, and a new test covers this. The worker script is downloaded with the decoder, so a failed fetch is retried instead of being fatal. The duplicated worker comment is gone.

@tridge
tridge marked this pull request as ready for review November 30, 2026 23:28
@tridge
tridge merged commit 24bd301 into ArduPilot:main Nov 30, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AIReview Request an automated AI review; picked up by the reviewprs sweep

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants