diff options
| -rw-r--r-- | src/features/messages.js | 37 | ||||
| -rw-r--r-- | src/style.css | 2 | ||||
| -rw-r--r-- | tools/feed-media-cache-test.js | 37 |
3 files changed, 59 insertions, 17 deletions
diff --git a/src/features/messages.js b/src/features/messages.js index 3be79fa..a0e9f96 100644 --- a/src/features/messages.js +++ b/src/features/messages.js @@ -5509,7 +5509,9 @@ export function messagesFeature(ctx) { const have = videoInfo.get(f.url) || {}; const d = {}; if (!have.size && +f.size > 0) d.size = +f.size; - const m = /^(\d+)x(\d+)$/.exec(f.dim || ''); + // no size, but a bitrate (bits/s) and a duration (Primal posts these): near enough + else if (!have.size && +f.bitrate > 0 && +f.duration > 0) d.size = Math.round(+f.bitrate * +f.duration / 8); + const m = /^(\d+)(?:\.0+)?x(\d+)(?:\.0+)?$/.exec(f.dim || ''); if (!have.w && m) { d.w = +m[1]; d.h = +m[2]; } if (!have.image && /^https?:\/\//.test(f.image || '')) d.image = f.image; if (Object.keys(d).length) noteVideoInfo(f.url, d); @@ -5548,7 +5550,10 @@ export function messagesFeature(ctx) { const have = videoInfo.get(url); // a post that states size and shape is taken at its word: no request at // all — unless the file's layout is wanted (a thumbnail) - if (have && (have.probed || have.failed || (!layout && have.size && have.w))) return Promise.resolve(have); + // a host that didn't answer is asked again after an hour (a phone on + // a bad connection must not mark a clip unmeasurable for good) + const failedLately = have && typeof have.failed === 'number' && Date.now() - have.failed < 3_600_000; + if (have && (have.probed || failedLately || (!layout && have.size && have.w))) return Promise.resolve(have); if (videoProbes.has(url)) return videoProbes.get(url); const task = new Promise((resolve) => videoProbeQueue.push({ url, resolve })).finally(() => videoProbes.delete(url)); videoProbes.set(url, task); @@ -5565,7 +5570,7 @@ export function messagesFeature(ctx) { } async function runVideoProbe(url) { const ctrl = new AbortController(); - const timer = setTimeout(() => ctrl.abort(), 6000); + const timer = setTimeout(() => ctrl.abort(), 10_000); try { const r = await fetch(url, { headers: { Range: 'bytes=0-' + (VIDEO_PROBE_BYTES - 1) }, signal: ctrl.signal }); if (!r.ok) throw new Error('HTTP ' + r.status); @@ -5585,13 +5590,22 @@ export function messagesFeature(ctx) { let o = 0; for (const p of parts) { if (o >= buf.length) break; buf.set(p.subarray(0, buf.length - o), o); o += p.length; } const head = readMp4Head(buf); - const d = { probed: true, ranges, faststart: head.faststart === true }; + const d = { probed: true, ranges, faststart: head.faststart === true, failed: undefined }; if (total > 0) d.size = total; + else { + // a server that hides Content-Range from scripts (Primal's) still + // says Content-Length to a HEAD, which any page may read + try { + const hr = await fetch(url, { method: 'HEAD', signal: AbortSignal.timeout(5000) }); + const n = +hr.headers.get('content-length'); + if (hr.ok && n > 0) d.size = n; + } catch {} + } if (head.w && !(videoInfo.get(url) || {}).w) { d.w = head.w; d.h = head.h; } if (head.w) rememberVideo(url, head.w, head.h); noteVideoInfo(url, d); } catch { - noteVideoInfo(url, { failed: true }); // a host that won't say (no CORS, down): asked again next session only + noteVideoInfo(url, { failed: Date.now() }); // a host that won't say (no CORS, down, slow): asked again in an hour } finally { clearTimeout(timer); } return videoInfo.get(url); } @@ -9797,7 +9811,18 @@ export function messagesFeature(ctx) { probeVideo(url, true).then((d) => { if (!v.isConnected) return; const heavy = videoHeavy(url); - const still = d && (d.probed ? d.faststart || d.ranges : !heavy); + // unmeasured (the probe failed): try for a frame anyway, but give + // up and let go after eight seconds, so an index-last file can't + // stream whole into a tile + const still = d && (d.probed ? d.faststart || d.ranges : true); + if (still && !(d && d.probed)) { + const cap = setTimeout(() => { + if (v.readyState >= 2 || !v.isConnected) return; + try { v.removeAttribute('src'); v.load(); } catch {} + v.replaceWith(h('span', { class: 'media-tile-cost' }, '')); + }, 8000); + v.addEventListener('loadeddata', () => clearTimeout(cap), { once: true }); + } if (still) { v.muted = true; // readable pixels, so the frame can be kept; a host that refuses diff --git a/src/style.css b/src/style.css index be15a80..b871bd4 100644 --- a/src/style.css +++ b/src/style.css @@ -1458,7 +1458,7 @@ body::after { .note-video-box .vid-heavy { min-height: 120px; } .media-tile-cost { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; - color: var(--muted); font-size: 12px; font-weight: 600; background: var(--surface2); + color: rgba(255, 255, 255, .8); font-size: 12px; font-weight: 600; background: #16181d; } .media-tile-badge { position: absolute; left: 6px; bottom: 6px; padding: 1px 6px; border-radius: 6px; diff --git a/tools/feed-media-cache-test.js b/tools/feed-media-cache-test.js index 08c679e..a1e0017 100644 --- a/tools/feed-media-cache-test.js +++ b/tools/feed-media-cache-test.js @@ -14,28 +14,33 @@ let ok = true; const check = (n, c, d = '') => { console.log(` ${c ? '✓' : '✗'} ${n}${d ? ' — ' + d : ''}`); if (!c) ok = false; }; const dir = mkdtempSync(tmpdir() + '/mcache-'); const ff = (args) => { const p = Bun.spawnSync(['ffmpeg', '-y', '-loglevel', 'error', ...args]); if (p.exitCode !== 0) throw new Error('ffmpeg failed'); }; -for (const n of ['v1', 'v2', 'v3']) ff(['-f', 'lavfi', '-i', 'testsrc=size=360x640:rate=10', '-t', '2', '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-movflags', '+faststart', `${dir}/${n}.mp4`]); +for (const n of ['v1', 'v2', 'v3', 'h4', 'n5']) ff(['-f', 'lavfi', '-i', 'testsrc=size=360x640:rate=10', '-t', '2', '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-movflags', '+faststart', `${dir}/${n}.mp4`]); ff(['-f', 'lavfi', '-i', 'testsrc=size=800x600', '-frames:v', '1', `${dir}/slow.jpg`]); const html = await buildHtml({ minify: true, pwa: false }); const server = Bun.serve({ port: 0, idleTimeout: 30, fetch: async (req) => { const path = new URL(req.url).pathname; const cors = { 'access-control-allow-origin': '*', 'access-control-expose-headers': 'content-range, content-length' }; - if (path.startsWith('/v/')) { + // /h/: like Primal's, scripts may read the bytes but not Content-Range; + // /n/: no CORS at all (a player may show it, a script can't read it) + if (path.startsWith('/v/') || path.startsWith('/h/') || path.startsWith('/n/')) { const f = Bun.file(dir + path.slice(2)); + const head = path.startsWith('/v/') ? cors : path.startsWith('/h/') ? { 'access-control-allow-origin': '*' } : {}; + if (req.method === 'HEAD') return new Response(null, { headers: { ...head, 'content-type': 'video/mp4', 'content-length': String(f.size) } }); const m = /bytes=(\d+)-(\d*)/.exec(req.headers.get('range') || ''); if (m) { const a = +m[1], b = Math.min(m[2] ? +m[2] : f.size - 1, f.size - 1); - return new Response(f.slice(a, b + 1), { status: 206, headers: { ...cors, 'content-type': 'video/mp4', 'content-range': `bytes ${a}-${b}/${f.size}`, 'accept-ranges': 'bytes' } }); + return new Response(f.slice(a, b + 1), { status: 206, headers: { ...head, 'content-type': 'video/mp4', 'content-range': `bytes ${a}-${b}/${f.size}`, 'accept-ranges': 'bytes' } }); } - return new Response(f, { headers: { ...cors, 'content-type': 'video/mp4' } }); + return new Response(f, { headers: { ...head, 'content-type': 'video/mp4' } }); } // a picture that takes ten seconds: the crawl must not wait on it if (path === '/i/slow.jpg') { await sleep(10000); return new Response(Bun.file(dir + '/slow.jpg'), { headers: { 'content-type': 'image/jpeg' } }); } if (path.startsWith('/punks') || path === '/verify-worker.js') return new Response(Bun.file('dist' + path)); return new Response(html, { headers: { 'content-type': 'text/html' } }); } }); -const base = server.url.origin; +const page0 = server.url.origin; +const base = page0.replace('localhost', '127.0.0.1'); // the media lives on another origin, as it does for real const SK = generateSecretKey(), PK = getPublicKey(SK); const now = Math.floor(Date.now() / 1000); const post = (i, content) => finalizeEvent({ kind: 1, created_at: now - 100 - i * 60, tags: [], content }, SK); @@ -48,6 +53,11 @@ for (let i = 42; i < 60; i++) all.push(post(i, 'more words ' + i)); all.push(post(60, 'second clip ' + base + '/v/v2.mp4')); for (let i = 61; i < 150; i++) all.push(post(i, 'deep words ' + i)); all.push(post(150, 'third clip, far down ' + base + '/v/v3.mp4')); +// the shape from imeta (the probe can't read it from either host) +const tagged = (i, content, url) => finalizeEvent({ kind: 1, created_at: now - 100 - i * 60, content, + tags: [['imeta', 'url ' + url, 'm video/mp4', 'dim 1080.0x1920.0', 'duration 2.0', 'bitrate 48000']] }, SK); +all.push(tagged(151, 'size hidden ' + base + '/h/h4.mp4', base + '/h/h4.mp4')); +all.push(tagged(152, 'no cors ' + base + '/n/n5.mp4', base + '/n/n5.mp4')); const browser = await puppeteer.launch({ executablePath: process.env.CHROME || '/usr/bin/google-chrome', headless: 'new', args: ['--no-sandbox', '--autoplay-policy=no-user-gesture-required'] }); const page = await browser.newPage(); @@ -94,7 +104,7 @@ const openFeed = async () => { try { await page.setViewport({ width: 390, height: 844 }); - await page.goto(base, { waitUntil: 'domcontentloaded' }); + await page.goto(page0, { waitUntil: 'domcontentloaded' }); await page.evaluate(() => localStorage.setItem('btc-wallet-network', 'regtest')); await page.reload({ waitUntil: 'domcontentloaded' }); await sleep(400); await click('create a new wallet'); await sleep(600); @@ -117,14 +127,21 @@ try { console.log('\n[a crawl straight into the media index]'); const t0 = Date.now(); await page.evaluate(() => { window.__reqs = []; document.querySelector('.media-mode[data-mode="videos"]').click(); }); - check('all three clips are found, the deepest 150 posts down', await waitFor(() => document.querySelectorAll('.media-grid .media-tile.video').length === 3, 15000), String(await tiles())); + check('all five clips are found, the deepest 150 posts down', await waitFor(() => document.querySelectorAll('.media-grid .media-tile.video').length === 5, 15000), String(await tiles())); const took = Date.now() - t0; check('...without waiting on the slow picture along the way', took < 8000, took + ' ms'); const reqs = await page.evaluate(() => window.__reqs); const steps = [...new Set(reqs.map((r) => r.until))]; // each step asks every relay check('...asking a hundred posts at a time, each step older than the last', reqs.length >= 1 && reqs.every((r) => r.limit === 100 && r.until) && steps.every((u, i) => !i || u < steps[i - 1]), JSON.stringify(steps)); - check('tile frames are kept as pictures', await waitFor(async () => (await (await caches.open('coinos-video-thumbs-v1')).keys()).length === 3, 8000)); + check('tile frames are kept as pictures (all but the no-CORS host\'s)', await waitFor(async () => (await (await caches.open('coinos-video-thumbs-v1')).keys()).length === 4, 8000)); + await waitFor(() => /h4\.mp4[^\]]*probed|probed[^\]]*h4/.test(localStorage.getItem('coinos-video-info') || '') && /n5\.mp4[^\]]*failed/.test(localStorage.getItem('coinos-video-info') || ''), 8000); // the write-behind + const info = await page.evaluate(() => Object.fromEntries(JSON.parse(localStorage.getItem('coinos-video-info') || '[]').map(([u, d]) => [u.split('/').pop(), d]))); + check('a host hiding Content-Range gives its size to a HEAD', info['h4.mp4']?.probed && info['h4.mp4'].size > 1000 && info['h4.mp4'].size !== 12000, JSON.stringify(info['h4.mp4'])); + check('a host refusing the probe still gets a frame in its tile', await page.evaluate(() => { + const v = [...document.querySelectorAll('.media-tile video')].find((x) => /n5/.test(x.getAttribute('data-src'))); + return !!v && v.readyState >= 2 && v.videoWidth > 0; }), JSON.stringify(info['n5.mp4'])); + check('...and its failure is dated, to be asked again later', typeof info['n5.mp4']?.failed === 'number'); await sleep(2000); // the index's write-behind console.log('\n[a reload]'); @@ -133,8 +150,8 @@ try { await page.evaluate(() => { window.__reqs = []; }); const t1 = Date.now(); await openFeed(); - check('the grid paints from the index at once', await waitFor(() => document.querySelectorAll('.media-grid .media-tile.video').length === 3, 4000), (Date.now() - t1) + ' ms'); - check('...its tiles pictures from the kept frames, no player needed', await waitFor(() => [...document.querySelectorAll('.media-grid .media-tile.video img')].filter((i) => i.src.startsWith('blob:')).length === 3, 4000)); + check('the grid paints from the index at once', await waitFor(() => document.querySelectorAll('.media-grid .media-tile.video').length === 5, 4000), (Date.now() - t1) + ' ms'); + check('...its tiles pictures from the kept frames, no player needed', await waitFor(() => [...document.querySelectorAll('.media-grid .media-tile.video img')].filter((i) => i.src.startsWith('blob:')).length === 4, 4000)); await sleep(2500); const after = await page.evaluate(() => window.__reqs.filter((r) => r.limit === 100)); const deepest = all.find((e) => /far down/.test(e.content)).created_at; |
