Introduce PlayerAdapter contract; decision logic shared in Rust backend

Establish a decoupled player boundary so UI and backend interact with video
through one contract, with the HTML5 (Linux/interim-Android) and native
(ExoPlayer) providers as interchangeable primitive-executor adapters.

- PlayerAdapter interface + AdapterHost callback bag (adapters/types.ts): the
  adapter owns only decision-free element PRIMITIVES (seekElement, reloadSource,
  play/pause, setVolume, selectSubtitle); it never branches on strategy.
- Seek/audio-track DECISIONS stay in Rust (player_seek_video / _switch_audio_track
  return a strategy); the facade dispatches the chosen primitive to the active
  adapter. Both providers share the one decision path — logic lives once, in Rust.
- Facade holds the active adapter; a new ControlCommand PlayerStatusEvent lets
  backend control (lockscreen/remote/sleep) drive the webview <video> element.
- Html5PlayerAdapter resolves the LIVE element via the bridge (fixes play/pause
  silently no-opping when the element was re-bound).
- Do not emit a "stopped" player state on natural end-of-video: it flipped the
  player/mode to idle mid-handoff and suppressed next-episode auto-advance under
  a sleep timer. Jellyfin progress reporting is preserved; the backend's
  on_video_playback_ended owns the transition.
- VideoPlayer net -300 lines (strategy/HLS-reload logic relocated to the adapter).
- Adds 20 adapter unit tests; existing suites stay green (vitest 457, cargo 416).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-02 19:56:20 +02:00
co-authored by Claude Opus 4.8
parent 1f6977cd01
commit a64e1b1fb4
15 changed files with 1198 additions and 301 deletions
+13 -78
View File
@@ -1,84 +1,19 @@
/**
* HTML5 <video> → Rust reporting adapter ("html5+rust internal module").
* Compatibility shim.
*
* On platforms where video renders in the webview (Linux WebKitGTK HTML5
* <video>; and, per the current interim behavior, Android too), the real player
* is the DOM element, which the Rust backend cannot observe directly. This
* module is the single place that reports the element's lifecycle back into
* Rust, so the `PlayerController` stays the source of truth and the frontend
* `player` store is fed from ONE pipeline (playerEvents.ts) in both native and
* HTML5 modes.
*
* The VideoPlayer component owns the element and its UI; it calls these
* functions from its DOM event handlers. Keeping the `commands.playerReport*`
* calls here (rather than scattered in the component) is the boundary: UI code
* never talks to the report commands directly.
*
* TRACES: UR-003, UR-005 | DR-001, DR-028
* The HTML5 → Rust reporting functions moved to `adapters/rustReportHost.ts` as
* part of the PlayerAdapter refactor. Existing callers import the reporter as
* `import * as html5Adapter from "$lib/player/html5Adapter"`; this shim keeps
* that working while the migration proceeds. New adapter code should depend on
* the `AdapterHost` interface (see `adapters/types.ts`) instead.
*/
import { commands } from "$lib/api/bindings";
export {
reportState,
reportPosition,
reportMediaLoaded,
resetReporting,
} from "./adapters/rustReportHost";
/** Player states mirrored to Rust (must match the strings playerEvents.ts handles). */
/** @deprecated states are defined on the AdapterHost interface now. */
export type Html5PlayerState = "playing" | "paused" | "loading" | "stopped" | "idle";
/**
* Report an HTML5 <video> state transition to Rust. The controller re-emits a
* `StateChanged` event identical to the native backends', so the frontend
* player store updates through its normal path.
*/
export async function reportState(
state: Html5PlayerState,
mediaId: string | null
): Promise<void> {
try {
await commands.playerReportState(state, mediaId);
} catch (err) {
console.warn("[html5Adapter] Failed to report state:", err);
}
}
/**
* Position reporting is throttled to ~250ms to match the native backends'
* cadence and avoid flooding the IPC channel from the 60fps RAF loop.
*/
let lastPositionReport = 0;
const POSITION_REPORT_INTERVAL_MS = 250;
/**
* Report an HTML5 <video> position tick to Rust (throttled). Safe to call every
* animation frame; only forwards at most every {@link POSITION_REPORT_INTERVAL_MS}.
*/
export async function reportPosition(
position: number,
duration: number,
{ force = false }: { force?: boolean } = {}
): Promise<void> {
const now = Date.now();
if (!force && now - lastPositionReport < POSITION_REPORT_INTERVAL_MS) {
return;
}
lastPositionReport = now;
try {
await commands.playerReportPosition(position, Number.isFinite(duration) ? duration : 0);
} catch (err) {
console.warn("[html5Adapter] Failed to report position:", err);
}
}
/**
* Report that the HTML5 <video> finished loading metadata and knows its
* duration. Mirrors the native `MediaLoaded` event.
*/
export async function reportMediaLoaded(duration: number): Promise<void> {
try {
await commands.playerReportMediaLoaded(Number.isFinite(duration) ? duration : 0);
} catch (err) {
console.warn("[html5Adapter] Failed to report media loaded:", err);
}
}
/** Reset internal throttle state (call when a new stream loads). */
export function resetReporting(): void {
lastPositionReport = 0;
}