Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/core/src/runtime/init.buffering.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ it("a clip that starves again after playing is waited for again", () => {
const { voiceReady, states, playFor } = playPodcast(0, "video", "", 30);
playFor(9);
voiceReady.value = 4;
voiceReady.bufferedTo = 11;
playFor(2);
expect(states.at(-1)).toEqual({ isPlaying: true, buffering: false });

Expand Down Expand Up @@ -315,6 +316,26 @@ it("a seek during a hold is not undone when a clip the hold reloaded loads later
expect(music.currentTime).toBe(landed);
});

it("a clip that gets no new data stays given up, even when it can play a moment from what it has", () => {
const { voiceReady, states, step, playFor } = playPodcast(0, "audio", "", 10);
voiceReady.value = 4;
voiceReady.bufferedTo = 2;
playFor(1);
voiceReady.value = 1;
playFor(9);
const holds = () => states.filter((s, i) => s.buffering && !states[i - 1]?.buffering).length;
const holdsAfterGivingUp = holds();
const gaveUpAt = window.__player!.getTime();

voiceReady.value = 4;
step(1000 / 60);
voiceReady.value = 1;
playFor(3);

expect(holds()).toBe(holdsAfterGivingUp);
expect(window.__player!.getTime()).toBeGreaterThan(gaveUpAt + 2.9);
});

it("a hold for data does not reload a later clip", () => {
const load = vi.spyOn(HTMLMediaElement.prototype, "load").mockImplementation(() => {});
const mark = document.createElement("meta");
Expand Down
13 changes: 10 additions & 3 deletions packages/core/src/runtime/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6353,7 +6353,13 @@ export function initSandboxRuntimeModular(): void {
};

const STOP_HOLDING_FOR_A_CLIP_WITHOUT_NEW_DATA_MS = 8000;
type HoldWithoutData = { src: string; buffered: number; sinceMs: number; checkedTick: number };
type HoldWithoutData = {
src: string;
buffered: number;
sinceMs: number;
checkedTick: number;
gaveUp?: boolean;
};
const holdsWithoutData = new WeakMap<HTMLMediaElement, HoldWithoutData>();
const stoppedReceivingData = (el: HTMLMediaElement): boolean => {
const now = performance.now();
Expand All @@ -6365,7 +6371,7 @@ export function initSandboxRuntimeModular(): void {
hold !== undefined &&
hold.src === el.currentSrc &&
buffered <= hold.buffered &&
hold.checkedTick >= transportTickCount - 1;
(hold.gaveUp === true || hold.checkedTick >= transportTickCount - 1);
if (!heldLastTick) {
holdsWithoutData.set(el, {
src: el.currentSrc,
Expand All @@ -6376,7 +6382,8 @@ export function initSandboxRuntimeModular(): void {
return false;
}
hold.checkedTick = transportTickCount;
return now - hold.sinceMs > STOP_HOLDING_FOR_A_CLIP_WITHOUT_NEW_DATA_MS;
hold.gaveUp ||= now - hold.sinceMs > STOP_HOLDING_FOR_A_CLIP_WITHOUT_NEW_DATA_MS;
return hold.gaveUp;
};

const mediaNotReadyAt = (t: number, ready = hasDataAhead): HTMLMediaElement[] =>
Expand Down
Loading