diff --git a/docs/RENDERER-CONTRACT.md b/docs/RENDERER-CONTRACT.md index d2e33cacc..504e27e7d 100644 --- a/docs/RENDERER-CONTRACT.md +++ b/docs/RENDERER-CONTRACT.md @@ -217,7 +217,10 @@ 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. 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` +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` 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 dc13d6fbe..c3b972e5e 100644 --- a/src/render/cesium/imageryFrames.js +++ b/src/render/cesium/imageryFrames.js @@ -23,6 +23,11 @@ export const VIDEO_STEPS_MAX = 1200; 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; +/** + * How long the seek past its end that learns a video's length may take, ms: + * it may read the whole video first. + */ +export const VIDEO_DURATION_TIMEOUT_MS = 60000; /** * The frames of a timed source, in time order: `{time, url}`, and for @@ -127,9 +132,9 @@ 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. Rejects on an error, or - * after `VIDEO_SEEK_TIMEOUT_MS` with no seek. + * after `timeoutMs` with no seek. */ -function seekTo(video, time, url) { +function seekTo(video, time, url, timeoutMs = VIDEO_SEEK_TIMEOUT_MS) { return new Promise((resolve, reject) => { const stop = () => { clearTimeout(timeout); @@ -140,7 +145,7 @@ function seekTo(video, time, url) { stop(); reject(new Error(`The video tile did not seek: ${url}`)); }; - const timeout = setTimeout(failed, VIDEO_SEEK_TIMEOUT_MS); + const timeout = setTimeout(failed, timeoutMs); const seeked = () => { if (video.seeking) return; stop(); @@ -202,10 +207,14 @@ export function createTileVideos(limit = VIDEO_CACHE) { // seeked past its end once to learn it. const ready = loaded.then(async () => { if (!Number.isFinite(video.duration)) - await seekTo(video, Number.MAX_SAFE_INTEGER, url); + await seekTo( + video, + Number.MAX_SAFE_INTEGER, + url, + VIDEO_DURATION_TIMEOUT_MS, + ); return video; }); - ready.catch(() => {}); video.src = url; let releaseWith; const gone = new Promise((_, reject) => { @@ -221,6 +230,14 @@ 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. + const loadedEntry = entry; + ready.catch(() => { + if (videos.get(url) !== loadedEntry) return; + videos.delete(url); + release(loadedEntry, url); + }); while (videos.size > limit) { const [oldest, old] = videos.entries().next().value; videos.delete(oldest); @@ -238,7 +255,9 @@ export function createTileVideos(limit = VIDEO_CACHE) { 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) + // 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)); const canvas = document.createElement('canvas'); canvas.width = video.videoWidth || 1; diff --git a/src/render/cesium/imageryFrames.test.mjs b/src/render/cesium/imageryFrames.test.mjs index 19cdc5424..f64e1650d 100644 --- a/src/render/cesium/imageryFrames.test.mjs +++ b/src/render/cesium/imageryFrames.test.mjs @@ -7,6 +7,7 @@ import { heldFrames, layerDrawn, shownAt, + VIDEO_DURATION_TIMEOUT_MS, VIDEO_SEEK_TIMEOUT_MS, VIDEO_STEP_MS, videoProvider, @@ -167,9 +168,17 @@ function fakeVideoPage() { createElement(tag) { if (tag === 'canvas') return { getContext: () => ({ drawImage() {} }) }; const target = new EventTarget(); + let time = 0; + // Setting the time starts a seek, which the test ends (`seeked`). + Object.defineProperty(target, 'currentTime', { + get: () => time, + set: (t) => { + time = t; + target.seeking = true; + }, + }); const video = Object.assign(target, { duration: NaN, - currentTime: 0, seeking: false, videoWidth: 2, videoHeight: 2, @@ -225,3 +234,70 @@ test('a tile video seek that never ends fails its frame after the seek timeout', restore(); } }); + +/** Let promise callbacks run. */ +const flush = async () => { + for (let i = 0; i < 10; i++) await Promise.resolve(); +}; + +/** End a fake video's seek. */ +const endSeek = (video) => { + video.seeking = false; + video.dispatchEvent(new Event('seeked')); +}; + +test('after a seek that timed out, the next frame seeks again rather than drawing what shows', async () => { + const { made, restore } = fakeVideoPage(); + mock.timers.enable({ apis: ['setTimeout'] }); + try { + const videos = createTileVideos(4); + const first = videos.frame('a.webm', 0.5); + first.catch(() => {}); + made[0].duration = 10; + made[0].dispatchEvent(new Event('loadeddata')); + 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. + let drawn = false; + const second = videos.frame('a.webm', 0.5).then(() => (drawn = true)); + await flush(); + assert.equal(drawn, false, 'waits for its own seek'); + endSeek(made[0]); + await second; + assert.equal(drawn, true); + } finally { + mock.timers.reset(); + restore(); + } +}); + +test('a video’s length is learnt with a longer wait, and a failed one is not kept', async () => { + const { made, restore } = fakeVideoPage(); + mock.timers.enable({ apis: ['setTimeout'] }); + try { + const videos = createTileVideos(4); + let failed = null; + videos.frame('a.webm', 0.5).catch((error) => (failed = error)); + // No duration: it seeks past its end to learn it. + made[0].dispatchEvent(new Event('loadeddata')); + await flush(); + mock.timers.tick(VIDEO_SEEK_TIMEOUT_MS); + await flush(); + assert.equal(failed, null, 'not on the short seek wait'); + 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. + const again = videos.frame('a.webm', 0.5); + assert.equal(made.length, 2, 'started over'); + made[1].duration = 10; + made[1].dispatchEvent(new Event('loadeddata')); + await flush(); + endSeek(made[1]); + assert.ok(await again); + } finally { + mock.timers.reset(); + restore(); + } +});