Playing a video meant asking the server to re-encode it, always. That
decision was made nowhere and written down nowhere, so whoever needed it
re-derived it downstream — the player worked out whether it had been handed
a playlist by looking for ".m3u8" in the URL, in two places. A viewer paid
for a transcode of a file their device could have played untouched, and the
app could not tell them which it was.
One negotiation now produces one self-describing StreamSelection — direct
play, remux or transcode; over a playlist, a plain HTTP file, or a local one
— and every renderer consumes that same answer.
Measured against the development server (Jellyfin 10.11.5), 400 items
sampled for codec mix and 40 put through a real PlaybackInfo negotiation
per profile:
Linux / WebKitGTK (h264 only, 2ch) 3/40 — 7% direct play
Android / ExoPlayer (hevc, ac3/eac3, 6ch) 34/40 — 85% direct play
The library is ~80% hevc, which is why the two diverge so hard. The payoff
is overwhelmingly Android, where 85% of plays were starting a transcode
nobody needed. Linux stays near 7% until libmpv decodes the picture — the
h264-only profile is a WebKitGTK constraint, not a JellyTau choice.
DR-219 StreamSelection: url + tagged Transport (hls/progressive/localFile)
+ PlaybackKind (directPlay/directStream/transcode) + the negotiated
rendition + this source's ladder + a needs_transcoding flag derived
in Rust so the rule is answered once. Both enums are serde-tagged
so the frontend matches a discriminant, not a substring. The paths
that never negotiate get the same shape from Rust rather than
assembling one — media_local_selection for a downloaded file,
LiveStreamInfo.transport for a live channel — so there is no second
place where a transport is decided.
DR-220 The ceiling becomes two levels: a durable device default (Settings,
persisted) and a per-playback override the in-player picker sets.
The picker had called itself a "this film, this connection" control
since it was written but wrote the process-wide default, so dropping
one awkward film to 2 Mbps silently capped every video played
afterwards for the rest of the process, with Settings still showing
the old value. The override is cleared whenever playback moves to a
new item, which stops it surviving into an autoplayed next episode.
effective_streaming_quality() is the single resolution point.
DR-221 The quality picker is filled from what this media source can offer.
Rust marks a rung exceeds_source when its ceiling is at or above the
source's own bitrate — such a rung is another way to spell Original
— and the frontend does not draw those. Original is never marked; a
source whose bitrate the server does not report marks nothing, which
keeps every rung offered.
DR-222 Direct play and direct stream are negotiated, with two client-side
overrides on top because the server's answer is right about the file
and wrong about what this app will do with it: undecodable audio
(Jellyfin 10.11.5 honours a DirectPlayProfile's container and video
codec but ignores its audio codec, so it offers direct play for an
E-AC-3 track the webview renders in silence) and a viewer-pinned
audio track the file does not default to. A direct stream is a remux
and is deliberately not counted as transcoding.
DR-223 Dropped on measurement, not deferred. A master playlist from this
server carries exactly one EXT-X-STREAM-INF: Jellyfin builds it from
the single rendition the request asked for rather than publishing a
ladder. So there is no adaptation for hls.js to be preserving and
none mpv would lose — the claim that there was, in
playback-backend-unification.md, does not hold. Recorded rather than
deleted because it is a measurement: a server that does publish a
ladder would change the answer.
DR-224 Every backend consumes the same selection. The queue item carries
the transport, so player_seek_video picks its seek strategy from the
backend's decision instead of the last stream_url.contains(".m3u8")
in the codebase. Items queued by a path that never negotiated carry
None and fall back to needs_transcoding, which is exact rather than
a guess because every transcode this app requests is HLS (DR-140).
The frontend loader decision moves to streamTransport.ts so it can be
tested: the two cases that pin it are the ones that failed against the old
implementation — a progressive stream whose URL contains ".m3u8" must not
get an HLS loader, and an HLS stream whose URL contains none must.
Also verified the URL the direct-play branch builds actually serves playable
bytes: 206, video/mp4, valid ISO-BMFF, and a mid-file range works, so
seeking a direct play works.
The spec is folded into docs/architecture/{01,02,03} and deleted, per the
rule that docs/specs holds only work that has not shipped. DR-121 leaves
read-through-media-cache.md with a pointer; that spec keeps its capture half.
Not verified: real playback on a device. Direct play changes what actually
gets played, and neither fixtures nor curl prove the WebKitGTK and ExoPlayer
paths render it.
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);
|
|
});
|
|
});
|