diff --git a/src/render/cesium/imageryFrames.js b/src/render/cesium/imageryFrames.js index 10dfa0b91..735649814 100644 --- a/src/render/cesium/imageryFrames.js +++ b/src/render/cesium/imageryFrames.js @@ -29,9 +29,9 @@ export const VIDEO_SEEK_TIMEOUT_MS = 10000; */ 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. + * How long a video that failed to load (to say how long it is, or to seek) + * 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; @@ -180,8 +180,18 @@ 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. */ + /** + * url → when a video that failed to load (or to seek) may be loaded again, + * epoch ms; pruned as it is added to. + */ const failed = new Map(); + /** Fail a url's frames at once for `VIDEO_RETRY_MS`. */ + function fail(url) { + const now = Date.now(); + for (const [other, retryAt] of failed) + if (retryAt <= now) failed.delete(other); + failed.set(url, now + VIDEO_RETRY_MS); + } /** 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; @@ -249,7 +259,7 @@ export function createTileVideos(limit = VIDEO_CACHE) { const loadedEntry = entry; ready.catch(() => { if (videos.get(url) !== loadedEntry) return; - failed.set(url, Date.now() + VIDEO_RETRY_MS); + fail(url); drop(url, loadedEntry); }); while (videos.size > limit) { @@ -283,8 +293,11 @@ export function createTileVideos(limit = VIDEO_CACHE) { 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. + // A video whose seek did not end (or failed) is let go and, like + // one that failed to load, loaded again only after + // `VIDEO_RETRY_MS`: a url that always hangs is not waited on by + // every tile that asks. One let go meanwhile has not failed. + if (!entry.released) fail(url); drop(url, entry); throw error; } @@ -302,6 +315,7 @@ export function createTileVideos(limit = VIDEO_CACHE) { clear() { for (const [url, entry] of videos) release(entry, url); videos.clear(); + failed.clear(); }, }; } diff --git a/src/render/cesium/imageryFrames.test.mjs b/src/render/cesium/imageryFrames.test.mjs index b1b70558b..c810ab485 100644 --- a/src/render/cesium/imageryFrames.test.mjs +++ b/src/render/cesium/imageryFrames.test.mjs @@ -247,9 +247,9 @@ const endSeek = (video) => { video.dispatchEvent(new Event('seeked')); }; -test('after a seek that timed out, the next frame loads a fresh video rather than drawing what shows', async () => { +test('after a seek that timed out, the video is not loaded again for a while, then afresh rather than drawing what shows', async () => { const { made, restore } = fakeVideoPage(); - mock.timers.enable({ apis: ['setTimeout'] }); + mock.timers.enable({ apis: ['setTimeout', 'Date'] }); try { const videos = createTileVideos(4); const first = videos.frame('a.webm', 0.5); @@ -260,6 +260,10 @@ test('after a seek that timed out, the next frame loads a fresh video rather tha mock.timers.tick(VIDEO_SEEK_TIMEOUT_MS); await assert.rejects(first, /did not seek/); assert.equal(made[0].src, '', 'the stuck video is let go'); + // Asked again soon: failed at once, never waited on for another timeout. + await assert.rejects(videos.frame('a.webm', 0.5), /failed to load lately/); + assert.equal(made.length, 1, 'not loaded again'); + mock.timers.tick(VIDEO_RETRY_MS); let drawn = false; const second = videos.frame('a.webm', 0.5).then(() => (drawn = true)); assert.equal(made.length, 2, 'a fresh video'); @@ -276,6 +280,29 @@ test('after a seek that timed out, the next frame loads a fresh video rather tha } }); +test('clear forgets the videos that failed, so each is loaded again at once', async () => { + const { made, restore } = fakeVideoPage(); + mock.timers.enable({ apis: ['setTimeout', 'Date'] }); + try { + const videos = createTileVideos(4); + const first = videos.frame('a.webm', 0.5); + made[0].dispatchEvent(new Event('error')); + await assert.rejects(first, /did not load/); + await assert.rejects(videos.frame('a.webm', 0.5), /failed to load lately/); + videos.clear(); + const again = videos.frame('a.webm', 0.5); + assert.equal(made.length, 2, 'loaded again'); + made[1].duration = 10; + made[1].dispatchEvent(new Event('loadeddata')); + await flush(); + endSeek(made[1]); + assert.ok(await again); + } finally { + mock.timers.reset(); + restore(); + } +}); + 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', 'Date'] });