feat(player): render Android video natively behind a transparent webview (DR-150, DR-151, DR-152)

Rust already reported `use_html5_element: false` on Android, but two frontend
overrides threw that answer away, so ExoPlayer's video path had never actually
run. Both are lifted behind an `experimentalNativeVideo` opt-in (default off).

The flag is a suppressor, never a promoter: off forces HTML5 even where Rust
says native, so an in-progress spike cannot ship as the default, but it can
never select native where Rust reported HTML5 — Linux cannot composite behind
WebKitGTK, and promoting there would be a black screen.

Two blockers the spec did not anticipate, both in code assumed to be merely
unreachable rather than broken:

- `JellyTauPlayer.setActivity()` had zero callers, so `currentActivity` was
  always null and `autoAttachSurface()` bailed. The SurfaceView was created and
  wired to ExoPlayer but never added to the view hierarchy — video would have
  decoded to a surface that was never on screen, whatever the webview did.
  This also revives PiP on the video path, which gated on the same flag.
- `createAdapter()` was not the real gate; it is never called in production.
  The actual override was in VideoPlayer.svelte, which forced HTML5 and stopped
  the native backend `player_play_item` had just started. Both sites now route
  through `createAdapter()`.

Compositing needs two independent opaque layers cleared, not one. Clearing only
the page leaves the WebView widget opaque — audio over a black picture, exactly
the symptom the old INTERIM comment described. `videoSurface.ts` toggles both:
the widget background and window drawable from Kotlin, the page backgrounds via
a `data-native-video` attribute keyed by app.css. Transparency lives in
`tauri.android.conf.json` so Linux keeps an opaque window, and is scoped to the
playback session so the launcher never shows through the rest of the app.

Phase 3's rect plumbing turned out to be unnecessary: video is fullscreen on the
player route, and `fitSurfaceToScreen()` already letterboxes and re-fits on
rotation. The mini-player transition remains unverified on device.

Also removes the `navigator.userAgent` sniffing in webviewAudio.ts, which was a
second copy of the Rust cfg gate free to drift from it. `player_get_capabilities`
now reports `usesWebviewAudio` and `supportsNativeVideo` from those same gates.

Tests: adapter selection covers the full matrix, including the regression guard
that the flag off beats Rust. Written first and confirmed failing (2 of 7) before
the fix.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-11 20:57:58 +02:00
co-authored by Claude Opus 5
parent 07d10dfed7
commit e144e62b31
16 changed files with 745 additions and 56 deletions
+86
View File
@@ -0,0 +1,86 @@
/**
* Native video surface compositing, Android only.
*
* TRACES: UR-003, UR-004 | DR-150, DR-151
*
* On Android, ExoPlayer renders video into a SurfaceView that sits *behind* the
* Tauri WebView (`setZOrderMediaOverlay(false)`, added at index 0 of the content
* view by VideoOverlayManager). For that video to be visible, two independent
* opaque layers have to be cleared:
*
* 1. The **WebView widget's own background** — reachable only from Kotlin, via
* the `AndroidVideoSurface` @JavascriptInterface installed by MainActivity.
* 2. The **web page's backgrounds** — the `html`/`body` colour in app.css and
* the app shell's `bg-[var(--color-background)]`. Handled by the
* `data-native-video` attribute, which $lib/stores/nativeVideo.ts sets and
* app.css keys its transparency rules off.
*
* Clearing only one leaves a black screen with audio, which is exactly the
* failure mode the old INTERIM override in VideoPlayer.svelte was working
* around. Both must be toggled together, so this module owns both halves.
*
* Everything here is a no-op off Android — the bridge is simply absent.
*/
import { nativeVideoActive } from "$lib/stores/nativeVideo";
interface AndroidVideoSurfaceBridge {
setTransparent(transparent: boolean): void;
isSupported(): boolean;
}
declare global {
interface Window {
AndroidVideoSurface?: AndroidVideoSurfaceBridge;
}
}
function bridge(): AndroidVideoSurfaceBridge | undefined {
if (typeof window === "undefined") return undefined;
return window.AndroidVideoSurface;
}
/**
* Whether the native-surface bridge exists on this platform. This reports only
* that the *plumbing* is present; whether native video should actually be used
* is Rust's decision (`player_get_capabilities`) gated by the user's
* `experimentalNativeVideo` flag.
*/
export function isNativeSurfaceBridgeAvailable(): boolean {
try {
return bridge()?.isSupported() ?? false;
} catch (err) {
console.warn("[videoSurface] isSupported check failed:", err);
return false;
}
}
/**
* Make the webview transparent so the video surface behind it shows through.
*
* MUST be paired with {@link disableNativeVideoCompositing} on teardown — a
* transparent window left behind shows the launcher through the whole app.
*/
export function enableNativeVideoCompositing(): void {
// Page layer first: if the Kotlin call succeeded but this threw, the user
// would see through the app to the home screen.
nativeVideoActive.set(true);
try {
bridge()?.setTransparent(true);
} catch (err) {
console.warn("[videoSurface] setTransparent(true) failed:", err);
nativeVideoActive.set(false);
}
}
/** Restore the opaque webview background. Safe to call unconditionally. */
export function disableNativeVideoCompositing(): void {
try {
bridge()?.setTransparent(false);
} catch (err) {
console.warn("[videoSurface] setTransparent(false) failed:", err);
}
// Always clear the page layer, even if the bridge call failed, so the app is
// never left rendering over a transparent window.
nativeVideoActive.set(false);
}