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>
104 lines
3.5 KiB
TypeScript
104 lines
3.5 KiB
TypeScript
/**
|
|
* Webview audio controller — the frontend half of audio-only playback on
|
|
* platforms with no native audio backend (currently Windows).
|
|
*
|
|
* The Rust `WebviewAudioBackend` emits a `webview_audio_load` event carrying the
|
|
* stream URL whenever a track loads. This controller owns a single hidden
|
|
* `<audio>` element, plays that URL through a {@link WebviewAudioAdapter}, and
|
|
* registers the adapter with the player facade so backend `control_command`
|
|
* events (play/pause/seek — routed by playerEvents.ts) reach the element. The
|
|
* adapter reports state/position back through the standard `player_report_*`
|
|
* round-trip, keeping the Rust controller the single source of truth.
|
|
*
|
|
* No-op on platforms with a native audio backend (Linux/Android): the backend
|
|
* never emits `webview_audio_load` there, so even if initialized this listener
|
|
* stays idle. We still gate initialization on platform to avoid mounting a stray
|
|
* element.
|
|
*
|
|
* TRACES: UR-003, UR-005 | DR-004
|
|
*/
|
|
|
|
import { type UnlistenFn } from "@tauri-apps/api/event";
|
|
import { events } from "$lib/api/bindings";
|
|
import { playerController } from "$lib/player";
|
|
import { createRustReportHost } from "$lib/player/adapters/rustReportHost";
|
|
import { WebviewAudioAdapter } from "$lib/player/adapters/webviewAudioAdapter";
|
|
import { getPlaybackCapabilities } from "$lib/services/playbackCapabilities";
|
|
|
|
let unlisten: UnlistenFn | null = null;
|
|
let audioEl: HTMLAudioElement | null = null;
|
|
let adapter: WebviewAudioAdapter | null = null;
|
|
|
|
/**
|
|
* Initialize the webview audio controller. Safe to call unconditionally from the
|
|
* root layout; it self-gates on platform and is idempotent.
|
|
*/
|
|
export async function initWebviewAudio(): Promise<void> {
|
|
if (unlisten) return;
|
|
|
|
// Whether this platform needs the webview element is a backend fact, so Rust
|
|
// answers it. This used to sniff `navigator.userAgent` for "android"/"linux"
|
|
// — a duplicate of the Rust cfg gate that could drift out of step with the
|
|
// backends it was trying to describe.
|
|
const { usesWebviewAudio } = await getPlaybackCapabilities();
|
|
if (!usesWebviewAudio) return;
|
|
|
|
audioEl = document.createElement("audio");
|
|
audioEl.hidden = true;
|
|
audioEl.preload = "auto";
|
|
// Kept in the DOM so the browser keeps decoding it when not focused.
|
|
document.body.appendChild(audioEl);
|
|
|
|
unlisten = await events.playerStatusEvent.listen((event) => {
|
|
const p = event.payload;
|
|
if (p.type !== "webview_audio_load") return;
|
|
void handleLoad(p.url, p.media_id, p.position, p.autoplay);
|
|
});
|
|
}
|
|
|
|
async function handleLoad(
|
|
url: string,
|
|
mediaId: string | null,
|
|
position: number,
|
|
autoplay: boolean
|
|
): Promise<void> {
|
|
if (!audioEl) return;
|
|
|
|
// Fresh host/adapter per load so reporting targets the current media id.
|
|
const host = createRustReportHost(mediaId ?? "", {});
|
|
adapter = new WebviewAudioAdapter(audioEl, host);
|
|
playerController.setActiveAdapter(adapter);
|
|
|
|
await adapter.load(url, {
|
|
mediaId: mediaId ?? "",
|
|
mediaSourceId: null,
|
|
needsTranscoding: false,
|
|
initialPosition: position,
|
|
isLive: false,
|
|
audioTrackIndex: null,
|
|
knownDuration: 0,
|
|
subtitleTracks: [],
|
|
});
|
|
|
|
if (!autoplay) {
|
|
await adapter.pause();
|
|
}
|
|
}
|
|
|
|
/** Tear down the controller (idempotent). */
|
|
export function cleanupWebviewAudio(): void {
|
|
if (unlisten) {
|
|
unlisten();
|
|
unlisten = null;
|
|
}
|
|
if (adapter) {
|
|
playerController.clearActiveAdapter(adapter);
|
|
void adapter.dispose();
|
|
adapter = null;
|
|
}
|
|
if (audioEl) {
|
|
audioEl.remove();
|
|
audioEl = null;
|
|
}
|
|
}
|