diff --git a/packages/core/src/runtime/init.buffering.test.ts b/packages/core/src/runtime/init.buffering.test.ts index 84ee600405..f46cdd0cb1 100644 --- a/packages/core/src/runtime/init.buffering.test.ts +++ b/packages/core/src/runtime/init.buffering.test.ts @@ -46,17 +46,17 @@ function audio(id: string, duration: number, readyState: { value: number; buffer return el; } -function playPodcast(voiceStart = 0, voiceTag = "audio", extraHtml = "") { +function playPodcast(voiceStart = 0, voiceTag = "audio", extraHtml = "", voiceDuration = 4) { resetRuntimeFixtureDom(); document.body.innerHTML = '
' + '' + - `<${voiceTag} id="voice" data-track-index="0" data-start="${voiceStart}" data-duration="4" src="/voice.m4a">` + + `<${voiceTag} id="voice" data-track-index="0" data-start="${voiceStart}" data-duration="${voiceDuration}" src="/voice.m4a">` + extraHtml + "
"; const voiceReady: { value: number; bufferedTo?: number } = { value: 1 }; const music = audio("music", 10, { value: 4 }); - const voice = audio("voice", 4, voiceReady); + const voice = audio("voice", voiceDuration, voiceReady); window.__timelines = { main: createMockTimeline(10) }; let now = 0; @@ -101,6 +101,57 @@ it("a track not ready pauses everything with Buffering, and resumes when it can expect(states.at(-1)).toEqual({ isPlaying: true, buffering: false }); }); +it("a clip that gets no data for 8 s stops holding the film, which plays on without it", () => { + const { music, states, playFor } = playPodcast(0, "video"); + + playFor(7); + expect(window.__player!.getTime()).toBeLessThan(0.1); + expect(states.at(-1)).toEqual({ isPlaying: true, buffering: true }); + + playFor(2); + expect(window.__player!.getTime()).toBeGreaterThan(0.5); + expect(music.paused).toBe(false); + expect(states.at(-1)).toEqual({ isPlaying: true, buffering: false }); + + // Seeking the stuck clip to keep up with the film brings no data, so it must not hold the film again. + const resumedStates = states.length; + playFor(10); + expect(window.__player!.getTime()).toBeGreaterThan(9); + expect(states.slice(resumedStates).some((s) => s.buffering)).toBe(false); +}); + +it("a stuck clip stops holding the film when ticks come a second apart, as in a hidden tab", () => { + const { states, step } = playPodcast(0, "video"); + + for (let s = 0; s < 10; s++) step(1100); + step(1000 / 60); + + expect(states.at(-1)).toEqual({ isPlaying: true, buffering: false }); +}); + +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; + playFor(2); + expect(states.at(-1)).toEqual({ isPlaying: true, buffering: false }); + + voiceReady.value = 1; + playFor(1); + expect(states.at(-1)).toEqual({ isPlaying: true, buffering: true }); +}); + +it("a clip still downloading keeps holding the film past 8 s", () => { + const { voiceReady, states, playFor } = playPodcast(0, "video"); + + for (let s = 0; s < 10; s++) { + voiceReady.bufferedTo = (s + 1) * 0.05; + playFor(1); + } + expect(window.__player!.getTime()).toBeLessThan(0.1); + expect(states.at(-1)).toEqual({ isPlaying: true, buffering: true }); +}); + it("the music plays on while a video shows a still across a copy swap", () => { const { music, voice, states, playFor } = playPodcast(0, "video"); heldPosters.set(voice, { poster: null, since: performance.now() }); diff --git a/packages/core/src/runtime/init.ts b/packages/core/src/runtime/init.ts index 7c94ac457f..ae27a4fa59 100644 --- a/packages/core/src/runtime/init.ts +++ b/packages/core/src/runtime/init.ts @@ -6183,6 +6183,33 @@ export function initSandboxRuntimeModular(): void { return false; }; + const STOP_HOLDING_FOR_A_CLIP_WITHOUT_NEW_DATA_MS = 8000; + type HoldWithoutData = { src: string; buffered: number; sinceMs: number; checkedTick: number }; + const holdsWithoutData = new WeakMap(); + const stoppedReceivingData = (el: HTMLMediaElement): boolean => { + const now = performance.now(); + let buffered = 0; + for (let i = 0; i < el.buffered.length; i++) + buffered += el.buffered.end(i) - el.buffered.start(i); + const hold = holdsWithoutData.get(el); + const heldLastTick = + hold !== undefined && + hold.src === el.currentSrc && + buffered <= hold.buffered && + hold.checkedTick >= transportTickCount - 1; + if (!heldLastTick) { + holdsWithoutData.set(el, { + src: el.currentSrc, + buffered, + sinceMs: now, + checkedTick: transportTickCount, + }); + return false; + } + hold.checkedTick = transportTickCount; + return now - hold.sinceMs > STOP_HOLDING_FOR_A_CLIP_WITHOUT_NEW_DATA_MS; + }; + const mediaNotReadyAt = (t: number, ready = hasDataAhead): HTMLMediaElement[] => state.nativeMediaSyncDisabled ? [] @@ -6191,6 +6218,7 @@ export function initSandboxRuntimeModular(): void { ({ el, start, end }) => isInClipWindow(t, start, end) && !ready(el) && + !stoppedReceivingData(el) && isPlayableTrack(el) && !stillStandsIn(el) && !pausedFrames.retained(el) &&