Cesium imageryFrames: a video whose seek never ends is let go; a failed one waits before loading again

A video whose seeks never ended stayed seeking, so every frame seeked it
again and waited out the timeout behind the others, and it was never
replaced. A seek that times out now lets the video go, so the next frame
loads a fresh one. A video that failed to load or to give its length
fails its frames at once for 3 minutes instead of being downloaded again
for every tile.
This commit is contained in:
Sameh Khamis committed 2026-11-11 20:23:28 -08:00
1 parent ba0fad3c35
commit 151230f5fa
3 files changed
+51 -13

No files matched your search

+34 -5
View File
@@ -28,6 +28,12 @@ export const VIDEO_SEEK_TIMEOUT_MS = 10000;
* it may read the whole video first.
*/
export const VIDEO_DURATION_TIMEOUT_MS = 60000;
/**
* How long a video that failed to load (or to say how long it is) fails its
* frames at once, ms, before it is loaded again: not downloaded afresh for
* every tile that asks.
*/
export const VIDEO_RETRY_MS = 180000;
/**
* The frames of a timed source, in time order: `{time, url}`, and for
@@ -174,6 +180,14 @@ function seekTo(video, time, url, timeoutMs = VIDEO_SEEK_TIMEOUT_MS) {
export function createTileVideos(limit = VIDEO_CACHE) {
/** url → {video, ready: Promise, queue: Promise, gone: Promise, release} */
const videos = new Map();
/** url → when a video that failed to load may be loaded again, epoch ms. */
const failed = new Map();
/** Let go of a url's video, if `entry` is still the one it holds. */
function drop(url, entry) {
if (videos.get(url) !== entry) return;
videos.delete(url);
release(entry, url);
}
/** Let go of an entry's video, failing what waits on it. */
function release(entry, url) {
entry.released = true;
@@ -230,13 +244,13 @@ export function createTileVideos(limit = VIDEO_CACHE) {
released: false,
};
videos.set(url, entry);
// A video that failed to load (or to say how long it is) is not kept:
// the next frame asked of it starts over.
// A video that failed to load (or to say how long it is) is not kept,
// and is loaded again only after `VIDEO_RETRY_MS`.
const loadedEntry = entry;
ready.catch(() => {
if (videos.get(url) !== loadedEntry) return;
videos.delete(url);
release(loadedEntry, url);
failed.set(url, Date.now() + VIDEO_RETRY_MS);
drop(url, loadedEntry);
});
while (videos.size > limit) {
const [oldest, old] = videos.entries().next().value;
@@ -247,6 +261,14 @@ export function createTileVideos(limit = VIDEO_CACHE) {
}
/** The video at `fraction` of its length, drawn on a canvas. */
function frame(url, fraction) {
const retryAt = failed.get(url);
if (retryAt !== undefined) {
if (Date.now() < retryAt)
return Promise.reject(
new Error(`The video tile failed to load lately: ${url}`),
);
failed.delete(url);
}
const entry = load(url);
// Whatever it waits on, it fails once its video is let go.
const until = (promise) => Promise.race([promise, entry.gone]);
@@ -258,7 +280,14 @@ export function createTileVideos(limit = VIDEO_CACHE) {
// Still seeking (a seek that timed out may have set the time without
// ever ending): seeked again, never drawn as it stands.
if (video.seeking || Math.abs(video.currentTime - at) > 1e-4)
await until(seekTo(video, at, url));
try {
await until(seekTo(video, at, url));
} catch (error) {
// A video whose seek did not end is let go: the next frame asked
// of it loads a fresh one rather than waiting on this one again.
drop(url, entry);
throw error;
}
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth || 1;
canvas.height = video.videoHeight || 1;
+14 -6
View File
@@ -8,6 +8,7 @@ import {
layerDrawn,
shownAt,
VIDEO_DURATION_TIMEOUT_MS,
VIDEO_RETRY_MS,
VIDEO_SEEK_TIMEOUT_MS,
VIDEO_STEP_MS,
videoProvider,
@@ -246,7 +247,7 @@ const endSeek = (video) => {
video.dispatchEvent(new Event('seeked'));
};
test('after a seek that timed out, the next frame seeks again rather than drawing what shows', async () => {
test('after a seek that timed out, the next frame loads a fresh video rather than drawing what shows', async () => {
const { made, restore } = fakeVideoPage();
mock.timers.enable({ apis: ['setTimeout'] });
try {
@@ -258,12 +259,15 @@ test('after a seek that timed out, the next frame seeks again rather than drawin
await flush();
mock.timers.tick(VIDEO_SEEK_TIMEOUT_MS);
await assert.rejects(first, /did not seek/);
// Its time is the target, but it never got there.
assert.equal(made[0].src, '', 'the stuck video is let go');
let drawn = false;
const second = videos.frame('a.webm', 0.5).then(() => (drawn = true));
assert.equal(made.length, 2, 'a fresh video');
made[1].duration = 10;
made[1].dispatchEvent(new Event('loadeddata'));
await flush();
assert.equal(drawn, false, 'waits for its own seek');
endSeek(made[0]);
endSeek(made[1]);
await second;
assert.equal(drawn, true);
} finally {
@@ -272,9 +276,9 @@ test('after a seek that timed out, the next frame seeks again rather than drawin
}
});
test('a video’s length is learnt with a longer wait, and a failed one is not kept', async () => {
test('a video’s length is learnt with a longer wait, and a failed one is not loaded again for a while', async () => {
const { made, restore } = fakeVideoPage();
mock.timers.enable({ apis: ['setTimeout'] });
mock.timers.enable({ apis: ['setTimeout', 'Date'] });
try {
const videos = createTileVideos(4);
let failed = null;
@@ -288,7 +292,11 @@ test('a video’s length is learnt with a longer wait, and a failed one is not k
mock.timers.tick(VIDEO_DURATION_TIMEOUT_MS);
await flush();
assert.match(String(failed), /did not seek/);
// Asked again: a fresh video, which loads this time.
// Asked again soon: failed at once, not downloaded again.
await assert.rejects(videos.frame('a.webm', 0.5), /failed to load lately/);
assert.equal(made.length, 1);
// Later: a fresh video, which loads this time.
mock.timers.tick(VIDEO_RETRY_MS);
const again = videos.frame('a.webm', 0.5);
assert.equal(made.length, 2, 'started over');
made[1].duration = 10;