Cesium imageryFrames: frames waiting on a tile video let go fail

An evicted (or cleared) tile video is emptied, so it never loads or seeks
again, and every frame queued on it waited forever, leaving its tiles
never finished. Letting a video go now fails what waits on it, and a seek
that does not end within 10 s fails its frame.
This commit is contained in:
Sameh Khamis committed 2026-11-11 19:30:31 -08:00
1 parent 9d3e99c831
commit a3ffb33563
3 files changed
+122 -22

No files matched your search

+3 -1
View File
@@ -214,7 +214,9 @@ at its share of the way, as a layer's `alpha` blends), with the next
`prefetch` frames (1 by default) loading unseen at alpha 0. A video source is
seen every 250 ms of each segment (at most 1,200 times a segment), each tile
the video its URL names for it (or one video over the layer's rectangle for a
`single-image` layer), seeked to the time's share of its length. `frameReady`
`single-image` layer), seeked to the time's share of its length. A layer
keeps its 48 most recently seen tile videos; a tile waiting on one it lets
go, or on a seek that takes over 10 s, fails rather than waiting. `frameReady`
names the nearest frame's time once every globe tile drawn shows that
frame's own imagery (`src/render/cesium/imageryFrames.js`). The checks
`globe.imagery.frames` and `globe.imagery.video` cover both.
+49 -20
View File
@@ -21,6 +21,8 @@ export const VIDEO_STEP_MS = 250;
export const VIDEO_STEPS_MAX = 1200;
/** Tile videos kept loaded a layer, the least recently seen let go first. */
export const VIDEO_CACHE = 48;
/** How long a tile video's seek may take before its frame fails, ms. */
export const VIDEO_SEEK_TIMEOUT_MS = 10000;
/**
* The frames of a timed source, in time order: `{time, url}`, and for
@@ -124,18 +126,24 @@ function tileUrlOf(template, tilingScheme, x, y, level) {
/**
* Seek a video, resolving once it has: on a `seeked` that leaves it no
* longer seeking (one a seek before it sends late does not count), and its
* frame there presented where the browser says so.
* frame there presented where the browser says so. Rejects on an error, or
* after `VIDEO_SEEK_TIMEOUT_MS` with no seek.
*/
function seekTo(video, time, url) {
return new Promise((resolve, reject) => {
const failed = () => {
video.removeEventListener('seeked', seeked);
reject(new Error(`The video tile did not seek: ${url}`));
};
const seeked = () => {
if (video.seeking) return;
const stop = () => {
clearTimeout(timeout);
video.removeEventListener('seeked', seeked);
video.removeEventListener('error', failed);
};
const failed = () => {
stop();
reject(new Error(`The video tile did not seek: ${url}`));
};
const timeout = setTimeout(failed, VIDEO_SEEK_TIMEOUT_MS);
const seeked = () => {
if (video.seeking) return;
stop();
if (typeof video.requestVideoFrameCallback !== 'function') resolve();
else {
// Its frame is drawable once presented; never wait on it for long.
@@ -147,18 +155,27 @@ function seekTo(video, time, url) {
}
};
video.addEventListener('seeked', seeked);
video.addEventListener('error', failed, { once: true });
video.addEventListener('error', failed);
video.currentTime = time;
});
}
/**
* A layer's tile videos, loaded once each and seeked for each frame drawn
* from them, one seek at a time a video.
* from them, one seek at a time a video. A video let go (the least recently
* seen past `limit`, or all of them on `clear`) fails every frame still
* waiting on it: emptied, it would never load or seek again.
*/
export function createTileVideos(limit = VIDEO_CACHE) {
/** url → {video, ready: Promise, queue: Promise} */
/** url → {video, ready: Promise, queue: Promise, gone: Promise, release} */
const videos = new Map();
/** Let go of an entry's video, failing what waits on it. */
function release(entry, url) {
entry.released = true;
entry.release(new Error(`The video tile was let go: ${url}`));
entry.video.removeAttribute('src');
entry.video.load();
}
function load(url) {
let entry = videos.get(url);
if (entry) {
@@ -190,24 +207,39 @@ export function createTileVideos(limit = VIDEO_CACHE) {
});
ready.catch(() => {});
video.src = url;
entry = { video, ready, queue: Promise.resolve() };
let releaseWith;
const gone = new Promise((_, reject) => {
releaseWith = reject;
});
gone.catch(() => {});
entry = {
video,
ready,
queue: Promise.resolve(),
gone,
release: releaseWith,
released: false,
};
videos.set(url, entry);
while (videos.size > limit) {
const [oldest, old] = videos.entries().next().value;
videos.delete(oldest);
old.video.removeAttribute('src');
old.video.load();
release(old, oldest);
}
return entry;
}
/** The video at `fraction` of its length, drawn on a canvas. */
function frame(url, fraction) {
const entry = load(url);
const run = entry.queue.then(async () => {
const video = await entry.ready;
// Whatever it waits on, it fails once its video is let go.
const until = (promise) => Promise.race([promise, entry.gone]);
const run = until(entry.queue).then(async () => {
if (entry.released) await entry.gone;
const video = await until(entry.ready);
const duration = Number.isFinite(video.duration) ? video.duration : 0;
const at = Math.max(0, Math.min(duration - 1e-3, fraction * duration));
if (Math.abs(video.currentTime - at) > 1e-4) await seekTo(video, at, url);
if (Math.abs(video.currentTime - at) > 1e-4)
await until(seekTo(video, at, url));
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth || 1;
canvas.height = video.videoHeight || 1;
@@ -220,10 +252,7 @@ export function createTileVideos(limit = VIDEO_CACHE) {
return {
frame,
clear() {
for (const { video } of videos.values()) {
video.removeAttribute('src');
video.load();
}
for (const [url, entry] of videos) release(entry, url);
videos.clear();
},
};
+70 -1
View File
@@ -1,11 +1,13 @@
import assert from 'node:assert/strict';
import * as Cesium from 'cesium';
import test from 'node:test';
import test, { mock } from 'node:test';
import {
createTileVideos,
framesOf,
heldFrames,
layerDrawn,
shownAt,
VIDEO_SEEK_TIMEOUT_MS,
VIDEO_STEP_MS,
videoProvider,
} from './imageryFrames.js';
@@ -156,3 +158,70 @@ test('a layer is drawn once every tile drawn shows its own imagery', () => {
false,
);
});
/** A page whose videos load (or never do) as the test says. */
function fakeVideoPage() {
const made = [];
const original = globalThis.document;
globalThis.document = {
createElement(tag) {
if (tag === 'canvas') return { getContext: () => ({ drawImage() {} }) };
const target = new EventTarget();
const video = Object.assign(target, {
duration: NaN,
currentTime: 0,
seeking: false,
videoWidth: 2,
videoHeight: 2,
src: '',
removeAttribute(name) {
if (name === 'src') video.src = '';
},
load() {},
});
made.push(video);
return video;
},
};
return { made, restore: () => (globalThis.document = original) };
}
test('frames waiting on a tile video that is let go fail, never wait forever', async () => {
const { made, restore } = fakeVideoPage();
try {
const videos = createTileVideos(1);
// Never loads.
const first = videos.frame('a.webm', 0);
const queued = videos.frame('a.webm', 0.5);
// A second video past the limit lets the first go.
const other = videos.frame('b.webm', 0);
assert.equal(made[0].src, '', 'the first is emptied');
await assert.rejects(first, /let go/);
await assert.rejects(queued, /let go/);
videos.clear();
await assert.rejects(other, /let go/);
} finally {
restore();
}
});
test('a tile video seek that never ends fails its frame after the seek timeout', async () => {
const { made, restore } = fakeVideoPage();
mock.timers.enable({ apis: ['setTimeout'] });
try {
const videos = createTileVideos(4);
const frame = videos.frame('a.webm', 0.5);
made[0].duration = 10;
made[0].dispatchEvent(new Event('loadeddata'));
// Let the load settle and the seek start; it never sends `seeked`.
for (let i = 0; i < 5; i++) await Promise.resolve();
let failed = null;
frame.catch((error) => (failed = error));
mock.timers.tick(VIDEO_SEEK_TIMEOUT_MS);
for (let i = 0; i < 5; i++) await Promise.resolve();
assert.match(String(failed), /did not seek/);
} finally {
mock.timers.reset();
restore();
}
});