mpv now decodes video on Linux, drawn into a framebuffer we own and blitted
into the default vbox's draw handler. Tauri's widget tree is untouched, so an
upgrade that assumes its own layout cannot invalidate this. Direct play means
the original file, hardware decoding, and no server transcode at all — where
previously every desktop video was re-encoded to h264 for the browser engine,
whatever the file actually was. Off by default: JELLYTAU_NATIVE_VIDEO=1.
That settles finding 2 of playback-backend-unification.md — "native video
cannot be composited with a Tauri webview" — by demonstration rather than
argument, on X11 and Wayland both.
Turning it on exposed nine defects, none of them mpv's. Each was the same
mistake in a different place: a capability written down as a compile-time fact
about the platform, or a state asserted instead of confirmed.
DR-238/246 a seek routed by the stream's container rather than by what the
engine could do with it - correct only while one player handled
those streams, silent the moment another did
DR-239 a property handled but never observed, so the play/pause button
waited for an event that could not arrive
DR-240 fullscreen expanding the document while the window stayed put
DR-241 a seek issued before the engine had a file, failed, and discarded
- which is why resume began at zero
DR-247 a Linux-only gate outliving the caller that made it Linux-only,
breaking the Android build outright
DR-250 a stop aimed at whichever renderer bookkeeping believed was in
charge, missing the one actually making sound
DR-251 a duration of zero believed, leaving the seek bar no scale
DR-252 a junk float converted to a Duration, panicking the backend the
instant a length-less stream appeared
So the MediaPlayer contract (DR-242 … DR-247): `open` carries a start position,
so no caller sequences load-then-seek and none can race an engine's load;
`seek` states a destination and leaves in-place-versus-re-open to the engine;
`snapshot` is one coherent read; and `Phase::Opening` names the window where
intent used to be lost. One conformance suite runs against every engine —
FakePlayer and mpv under cargo test, ExoPlayer instrumented on a device — so an
engine is either correct or visibly failing.
Two of the nine were introduced during this work and caught on hardware, not by
any suite: an over-broad capability that grouped ExoPlayer with mpv, and the
Duration panic. The suites test engines that behave. That is recorded in
docs/native-player-verification.md, which asks for the exact action sequences
that found them.
Verified: all automated gates, conformance (mpv 9/9, legacy 8/9 by design,
ExoPlayer 7/7 on device), and manual desktop and Android passes on real
hardware.
Known open and deliberately shipped: resume reads local progress and never the
server's; the background-audio handoff still declares a state swap it does not
confirm (the symptom is now impossible, the race is not); and `bun run
android:dev` builds an APK carrying the release application id, whose failure
message advises an uninstall that would destroy app data. Fix that last one
before anyone else builds for Android.
Squashed from worktree-linux-native-video, which keeps the per-defect history.
341 lines
12 KiB
TypeScript
341 lines
12 KiB
TypeScript
/**
|
|
* Unit tests for Html5PlayerAdapter.
|
|
*
|
|
* The Option-1 primitive design makes the adapter pure, decision-free mechanics
|
|
* — it takes a mock <video> element + bridge + host, so we can assert each
|
|
* primitive drives the element correctly without any real DOM or backend.
|
|
*/
|
|
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { Html5PlayerAdapter, type Html5ElementBridge } from "./html5Adapter";
|
|
import type { AdapterHost } from "./types";
|
|
|
|
/**
|
|
* A `StreamSelection` for tests that only care about the URL. Transcoded HLS is
|
|
* what these paths exercised before the contract carried a transport.
|
|
*/
|
|
function testSelection(url: string, transport: "hls" | "progressive" | "localFile" = "hls") {
|
|
return {
|
|
url,
|
|
transport: { type: transport },
|
|
playbackKind: { type: transport === "hls" ? "transcode" : "directPlay" },
|
|
rendition: null,
|
|
available: [],
|
|
mediaSourceId: null,
|
|
playSessionId: null,
|
|
needsTranscoding: transport === "hls",
|
|
} as import("$lib/api/bindings").StreamSelection;
|
|
}
|
|
|
|
/** A minimal fake <video> element that records mutations and fires events. */
|
|
function makeFakeVideo() {
|
|
const listeners: Record<string, Array<() => void>> = {};
|
|
const el: any = {
|
|
paused: true,
|
|
currentTime: 0,
|
|
volume: 1,
|
|
muted: false,
|
|
src: "blob:existing",
|
|
play: vi.fn(async () => {
|
|
el.paused = false;
|
|
}),
|
|
pause: vi.fn(() => {
|
|
el.paused = true;
|
|
}),
|
|
load: vi.fn(),
|
|
removeAttribute: vi.fn((attr: string) => {
|
|
if (attr === "src") el.src = "";
|
|
}),
|
|
addEventListener: (event: string, cb: () => void) => {
|
|
(listeners[event] ??= []).push(cb);
|
|
},
|
|
removeEventListener: (event: string, cb: () => void) => {
|
|
listeners[event] = (listeners[event] ?? []).filter((f) => f !== cb);
|
|
},
|
|
// Test helper: fire an event so waitForEvent resolves immediately.
|
|
_fire: (event: string) => {
|
|
(listeners[event] ?? []).slice().forEach((f) => f());
|
|
},
|
|
querySelectorAll: () => [] as any,
|
|
textTracks: [] as any,
|
|
};
|
|
return el;
|
|
}
|
|
type FakeVideo = ReturnType<typeof makeFakeVideo>;
|
|
|
|
function makeBridge(overrides: Partial<Html5ElementBridge> = {}): Html5ElementBridge {
|
|
let offset = 0;
|
|
return {
|
|
getElement: () => null,
|
|
getSeekOffset: () => offset,
|
|
setSeekOffset: vi.fn((o: number) => {
|
|
offset = o;
|
|
}),
|
|
setStreamSelection: vi.fn(),
|
|
destroyHls: vi.fn(),
|
|
getMediaSourceId: () => "msid-1",
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function makeHost(): AdapterHost {
|
|
return {
|
|
onState: vi.fn(),
|
|
onPosition: vi.fn(),
|
|
onMediaLoaded: vi.fn(),
|
|
onEnded: vi.fn(),
|
|
onError: vi.fn(),
|
|
onStreamUrlChanged: vi.fn(),
|
|
onBuffering: vi.fn(),
|
|
onReady: vi.fn(),
|
|
};
|
|
}
|
|
|
|
describe("Html5PlayerAdapter", () => {
|
|
let host: AdapterHost;
|
|
let bridge: Html5ElementBridge;
|
|
let adapter: Html5PlayerAdapter;
|
|
let video: ReturnType<typeof makeFakeVideo>;
|
|
|
|
beforeEach(() => {
|
|
host = makeHost();
|
|
bridge = makeBridge();
|
|
adapter = new Html5PlayerAdapter(host, bridge);
|
|
video = makeFakeVideo();
|
|
adapter.attach(video);
|
|
});
|
|
|
|
it("is an html5-kind adapter", () => {
|
|
expect(adapter.kind).toBe("html5");
|
|
});
|
|
|
|
it("play() calls element.play()", async () => {
|
|
await adapter.play();
|
|
expect(video.play).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
// A stalling HLS stream makes hls.js' gap-controller nudge the element, which
|
|
// aborts an in-flight play(). That AbortError is transient — the element is
|
|
// still trying to play — so it must not be surfaced as a player error, or the
|
|
// UI reports failure ~once a second for the whole stall.
|
|
it("play() does not report an interrupted-by-pause AbortError as an error", async () => {
|
|
const abort = new DOMException(
|
|
"The play() request was interrupted by a call to pause().",
|
|
"AbortError",
|
|
);
|
|
video.play = vi.fn(async () => {
|
|
throw abort;
|
|
});
|
|
|
|
await adapter.play();
|
|
|
|
expect(host.onError).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("play() still reports a genuine failure", async () => {
|
|
video.play = vi.fn(async () => {
|
|
throw new DOMException("no supported source", "NotSupportedError");
|
|
});
|
|
|
|
await adapter.play();
|
|
|
|
expect(host.onError).toHaveBeenCalledTimes(1);
|
|
expect(String((host.onError as any).mock.calls[0][0])).toContain("play() failed");
|
|
});
|
|
|
|
it("play() coalesces concurrent attempts into one element.play() call", async () => {
|
|
// During a stall the UI and recovery paths can both ask to play. Stacking
|
|
// element.play() calls is what generates the AbortError storm.
|
|
let resolvePlay: () => void = () => {};
|
|
video.play = vi.fn(
|
|
() =>
|
|
new Promise<void>((r) => {
|
|
resolvePlay = () => {
|
|
video.paused = false;
|
|
r();
|
|
};
|
|
}),
|
|
);
|
|
|
|
const first = adapter.play();
|
|
const second = adapter.play();
|
|
resolvePlay();
|
|
await Promise.all([first, second]);
|
|
|
|
expect(video.play).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("play() works again after a previous attempt settled", async () => {
|
|
await adapter.play();
|
|
await adapter.play();
|
|
expect(video.play).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it("pause() calls element.pause()", async () => {
|
|
video.paused = false;
|
|
await adapter.pause();
|
|
expect(video.pause).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("toggle() plays when paused and reports the resulting state", async () => {
|
|
video.paused = true;
|
|
const playing = await adapter.toggle();
|
|
expect(video.play).toHaveBeenCalled();
|
|
expect(playing).toBe(true);
|
|
});
|
|
|
|
it("toggle() pauses when playing", async () => {
|
|
video.paused = false;
|
|
const playing = await adapter.toggle();
|
|
expect(video.pause).toHaveBeenCalled();
|
|
expect(playing).toBe(false);
|
|
});
|
|
|
|
it("seekElement() sets currentTime, offset, and waits for 'seeked'", async () => {
|
|
const p = adapter.seekElement(42, 0);
|
|
expect(video.currentTime).toBe(42);
|
|
expect(bridge.setSeekOffset).toHaveBeenCalledWith(0);
|
|
video._fire("seeked"); // resolve the wait
|
|
await p;
|
|
});
|
|
|
|
it("reloadSource() runs the invariant teardown->swap->resume sequence", async () => {
|
|
video.paused = false; // was playing → should resume
|
|
const p = adapter.reloadSource(testSelection("http://new/master.m3u8"), 120);
|
|
|
|
// Teardown happened synchronously before the awaited canplay wait.
|
|
expect(video.pause).toHaveBeenCalled();
|
|
expect(bridge.destroyHls).toHaveBeenCalledTimes(1);
|
|
expect(video.removeAttribute).toHaveBeenCalledWith("src");
|
|
expect(video.load).toHaveBeenCalled();
|
|
|
|
// Allow the internal 100ms settle delay, then fire canplay to resume.
|
|
await new Promise((r) => setTimeout(r, 110));
|
|
expect(bridge.setStreamSelection).toHaveBeenCalledWith(
|
|
expect.objectContaining({ url: "http://new/master.m3u8", transport: { type: "hls" } }),
|
|
);
|
|
video._fire("canplay");
|
|
video._fire("seeked");
|
|
await p;
|
|
expect(video.play).toHaveBeenCalled(); // resumed because it was playing
|
|
});
|
|
|
|
/**
|
|
* The reload lands the viewer at the position they asked for — by *seeking*,
|
|
* with no transcode offset left over.
|
|
*
|
|
* This used to be inverted: the offset was set to the position and nothing
|
|
* seeked, which was right only while the reloaded URL itself began there via
|
|
* `StartTimeTicks`. DR-181 removes that parameter, because on an HLS playlist
|
|
* the server copies it onto every segment URI and then rejects each one with
|
|
* `400`. With the URL starting at the item's zero, the old arithmetic leaves
|
|
* `currentTime = offset + 0` — the scrubber reading 20:00 over the opening
|
|
* titles, and the seek silently never happening.
|
|
*
|
|
* TRACES: UR-004, UR-005 | DR-181 | UT-183
|
|
*/
|
|
it("reloadSource() seeks to the position and clears the transcode offset", async () => {
|
|
video.paused = false;
|
|
const p = adapter.reloadSource(testSelection("http://new/master.m3u8"), 1200);
|
|
|
|
await new Promise((r) => setTimeout(r, 110));
|
|
expect(bridge.setSeekOffset).toHaveBeenCalledWith(0);
|
|
expect(bridge.setSeekOffset).not.toHaveBeenCalledWith(1200);
|
|
|
|
// Nothing may seek before the new source is playable — the element drops it.
|
|
expect(video.currentTime).not.toBe(1200);
|
|
|
|
video._fire("canplay");
|
|
await new Promise((r) => setTimeout(r, 0));
|
|
expect(video.currentTime).toBe(1200);
|
|
|
|
video._fire("seeked");
|
|
await p;
|
|
expect(video.play).toHaveBeenCalled();
|
|
});
|
|
|
|
/** A reload to the very start has nothing to seek to; it must not stall. */
|
|
it("reloadSource() at position 0 does not wait for a seek", async () => {
|
|
video.paused = false;
|
|
const p = adapter.reloadSource(testSelection("http://new/master.m3u8"), 0);
|
|
await new Promise((r) => setTimeout(r, 110));
|
|
video._fire("canplay");
|
|
await p; // resolves without any "seeked" event
|
|
expect(video.play).toHaveBeenCalled();
|
|
});
|
|
|
|
/**
|
|
* A reload that never becomes playable must be reported as a failure. It used
|
|
* to resolve on the timeout, so a quality switch whose new stream the server
|
|
* refused to serve (Jellyfin 400s the first segment when two transcode jobs
|
|
* collide) looked like a success: the picker showed the new quality selected
|
|
* over a stream that never played, and the caller had nothing to revert to.
|
|
*
|
|
* TRACES: UR-074 | DR-177 | UT-175
|
|
*/
|
|
it("reloadSource() rejects when the new stream never becomes playable", async () => {
|
|
vi.useFakeTimers();
|
|
try {
|
|
video.paused = false;
|
|
const p = adapter.reloadSource(testSelection("http://new/master.m3u8"), 120);
|
|
const assertion = expect(p).rejects.toThrow(/canplay/i);
|
|
await vi.advanceTimersByTimeAsync(11_000); // past the 10s readiness budget
|
|
await assertion;
|
|
expect(video.play).not.toHaveBeenCalled(); // nothing to resume into
|
|
} finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
it("reloadSource() does not resume when it was paused", async () => {
|
|
video.paused = true;
|
|
const p = adapter.reloadSource(testSelection("http://new/master.m3u8"), 30);
|
|
await new Promise((r) => setTimeout(r, 110));
|
|
video._fire("canplay");
|
|
video._fire("seeked");
|
|
await p;
|
|
expect(video.play).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("setVolume() clamps to 0..1", () => {
|
|
adapter.setVolume(1.5);
|
|
expect(video.volume).toBe(1);
|
|
adapter.setVolume(-0.5);
|
|
expect(video.volume).toBe(0);
|
|
adapter.setVolume(0.4);
|
|
expect(video.volume).toBeCloseTo(0.4);
|
|
});
|
|
|
|
it("setMuted() sets the element muted flag", () => {
|
|
adapter.setMuted(true);
|
|
expect(video.muted).toBe(true);
|
|
});
|
|
|
|
it("getPosition() returns element time plus the transcode offset", () => {
|
|
video.currentTime = 10;
|
|
(bridge.getSeekOffset as any) = () => 100;
|
|
// Rebuild adapter with the offset-returning bridge.
|
|
const a = new Html5PlayerAdapter(host, bridge);
|
|
a.attach(video);
|
|
expect(a.getPosition()).toBe(110);
|
|
});
|
|
|
|
it("dispose() tears down hls and clears the element", async () => {
|
|
await adapter.dispose();
|
|
expect(bridge.destroyHls).toHaveBeenCalled();
|
|
expect(video.pause).toHaveBeenCalled();
|
|
// After dispose, primitives are no-ops (element detached).
|
|
await adapter.play();
|
|
// play was called once during dispose teardown? no — play only on reload/resume.
|
|
expect(video.play).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("primitives are safe no-ops before an element is attached", async () => {
|
|
const bare = new Html5PlayerAdapter(host, bridge);
|
|
await expect(bare.play()).resolves.toBeUndefined();
|
|
await expect(bare.pause()).resolves.toBeUndefined();
|
|
await expect(bare.seekElement(5, 0)).resolves.toBeUndefined();
|
|
expect(await bare.toggle()).toBe(false);
|
|
});
|
|
});
|