diff --git a/docs/RENDERER-CONTRACT.md b/docs/RENDERER-CONTRACT.md index 305f65dfb..18ecbe4f2 100644 --- a/docs/RENDERER-CONTRACT.md +++ b/docs/RENDERER-CONTRACT.md @@ -219,8 +219,9 @@ the video its URL names for it (or one video over the layer's rectangle for a 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 (60 s for the seek past its end that learns the length of a video that does not state it), fails rather than -waiting; a video that failed to load is asked for afresh by the next tile, and -one still seeking is seeked again rather than drawn as it stands. `frameReady` +waiting. A video whose seek did not end is let go, so the next tile loads a +fresh one; one that failed to load (or to say how long it is) fails its tiles +at once for 3 minutes, then loads again. `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. diff --git a/src/render/cesium/imageryFrames.js b/src/render/cesium/imageryFrames.js index c3b972e5e..10dfa0b91 100644 --- a/src/render/cesium/imageryFrames.js +++ b/src/render/cesium/imageryFrames.js @@ -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; diff --git a/src/render/cesium/imageryFrames.test.mjs b/src/render/cesium/imageryFrames.test.mjs index f64e1650d..b1b70558b 100644 --- a/src/render/cesium/imageryFrames.test.mjs +++ b/src/render/cesium/imageryFrames.test.mjs @@ -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;