Spaces:
Running
Running
JOHN SILLVESTOR commited on
Firefox keeps original Antz timing
Browse files
server.js
CHANGED
|
@@ -1831,6 +1831,12 @@ async function rangeSlicesReady(msgId, start, end) {
|
|
| 1831 |
}
|
| 1832 |
|
| 1833 |
const avLead = new Map();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1834 |
|
| 1835 |
function handlerNear(buf, from) {
|
| 1836 |
const end = Math.min(buf.length - 4, from + 180);
|
|
@@ -1852,8 +1858,49 @@ function timescaleNear(buf, from) {
|
|
| 1852 |
return 0;
|
| 1853 |
}
|
| 1854 |
|
| 1855 |
-
function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1856 |
if (!buf || buf.length < 32 || fileOff > 8 * MB) return buf;
|
|
|
|
| 1857 |
const id = String(msgId || "");
|
| 1858 |
let state = avLead.get(id);
|
| 1859 |
if (!state) {
|
|
@@ -1892,10 +1939,13 @@ function patchAvSync(buf, fileOff, msgId) {
|
|
| 1892 |
const e1mt = out.readInt32BE(mark + 28);
|
| 1893 |
if (e1dur > 0 && e1mt >= 0) {
|
| 1894 |
if (out === buf) out = Buffer.from(buf);
|
| 1895 |
-
out.writeUInt32BE(
|
| 1896 |
out.writeUInt32BE((e0dur + e1dur) >>> 0, mark + 12);
|
| 1897 |
out.writeInt32BE(e1mt, mark + 16);
|
|
|
|
|
|
|
| 1898 |
state.delay = e0dur;
|
|
|
|
| 1899 |
}
|
| 1900 |
} else if (kind === "soun" && state.delay > 0 && e0mt === 0 && ts > 0 && e0dur > state.delay) {
|
| 1901 |
const skip = Math.round(state.delay * ts / state.movieTs);
|
|
@@ -1940,7 +1990,8 @@ async function pipeDiskRange(msgId, start, end, req, res, headers, size) {
|
|
| 1940 |
if (buf.length > left) buf = buf.subarray(0, left);
|
| 1941 |
if (!buf.length) continue;
|
| 1942 |
hotFill(msgId, at, buf, size);
|
| 1943 |
-
|
|
|
|
| 1944 |
at += buf.length;
|
| 1945 |
const ok = res.write(out);
|
| 1946 |
if (!ok) await waitDrain(req, res);
|
|
@@ -2129,7 +2180,8 @@ async function pipeRange(bot, rec, start, end, req, res, status, headers, msgId)
|
|
| 2129 |
let pos = start;
|
| 2130 |
const prefix = hotPrefix(msgId, pos, end);
|
| 2131 |
if (prefix && prefix.length) {
|
| 2132 |
-
|
|
|
|
| 2133 |
const ok = res.write(patched);
|
| 2134 |
lastByte = Date.now();
|
| 2135 |
pos += prefix.length;
|
|
@@ -2141,7 +2193,8 @@ async function pipeRange(bot, rec, start, end, req, res, status, headers, msgId)
|
|
| 2141 |
for await (const buf of gen) {
|
| 2142 |
if (stop()) break;
|
| 2143 |
hotFill(msgId, at, buf, rec.size);
|
| 2144 |
-
|
|
|
|
| 2145 |
at += buf.length;
|
| 2146 |
const ok = res.write(out);
|
| 2147 |
lastByte = Date.now();
|
|
@@ -2196,6 +2249,8 @@ async function handleGenerate(req, res, url) {
|
|
| 2196 |
const body = Object.assign({}, ghit.body);
|
| 2197 |
const cd = durCache.get(String(msgId));
|
| 2198 |
if (cd > 1) body.d = Math.floor(cd);
|
|
|
|
|
|
|
| 2199 |
return json(res, 200, body);
|
| 2200 |
}
|
| 2201 |
}
|
|
@@ -2246,6 +2301,12 @@ async function handleGenerate(req, res, url) {
|
|
| 2246 |
const payload = { m: msgId, h: hash, u: uid || "anon", l: lvl, b: bot.idx, e: exp, s: rec.size, n: playMime, p: ipTag(ip), a: uaTag(req.headers["user-agent"]), d: durSec };
|
| 2247 |
const k = sealTicket(payload);
|
| 2248 |
const host = publicSpaceOrigin(req);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2249 |
const body = {
|
| 2250 |
u: host + "/p/" + k + ".mp4",
|
| 2251 |
d: durSec,
|
|
@@ -2253,6 +2314,7 @@ async function handleGenerate(req, res, url) {
|
|
| 2253 |
a: lvl,
|
| 2254 |
s: rec.size
|
| 2255 |
};
|
|
|
|
| 2256 |
if (!rawAuth && lvl === "anonymous") {
|
| 2257 |
genAnonCache.set(path + "|" + hash + "|a|" + ipTag(ip) + "|" + uaTag(req.headers["user-agent"]), { body, exp: Date.now() + 180000, ts: Date.now() });
|
| 2258 |
}
|
|
@@ -2313,7 +2375,8 @@ async function handleStream(req, res, url, msgId, kHex, ticketObj) {
|
|
| 2313 |
try { res.socket.setNoDelay(true); } catch { }
|
| 2314 |
}
|
| 2315 |
res.writeHead(206, headersHot);
|
| 2316 |
-
|
|
|
|
| 2317 |
return;
|
| 2318 |
}
|
| 2319 |
let pooled = null;
|
|
|
|
| 1831 |
}
|
| 1832 |
|
| 1833 |
const avLead = new Map();
|
| 1834 |
+
const elstLeadSec = new Map();
|
| 1835 |
+
|
| 1836 |
+
function geckoUA(ua) {
|
| 1837 |
+
const s = String(ua || "");
|
| 1838 |
+
return /Firefox\//i.test(s) && !/Seamonkey/i.test(s);
|
| 1839 |
+
}
|
| 1840 |
|
| 1841 |
function handlerNear(buf, from) {
|
| 1842 |
const end = Math.min(buf.length - 4, from + 180);
|
|
|
|
| 1858 |
return 0;
|
| 1859 |
}
|
| 1860 |
|
| 1861 |
+
function videoEmptyEditSec(buf) {
|
| 1862 |
+
if (!buf || buf.length < 32) return 0;
|
| 1863 |
+
let movieTs = 1000;
|
| 1864 |
+
const mv = buf.indexOf("mvhd");
|
| 1865 |
+
if (mv > 0 && mv + 24 < buf.length && buf[mv + 4] === 0) {
|
| 1866 |
+
const ts = buf.readUInt32BE(mv + 16);
|
| 1867 |
+
if (ts > 0 && ts < 1000000) movieTs = ts;
|
| 1868 |
+
}
|
| 1869 |
+
let from = 0;
|
| 1870 |
+
while (from + 12 < buf.length) {
|
| 1871 |
+
const mark = buf.indexOf("elst", from);
|
| 1872 |
+
if (mark < 4) break;
|
| 1873 |
+
const size = buf.readUInt32BE(mark - 4);
|
| 1874 |
+
if (size < 28 || size > 512 || mark - 4 + size > buf.length || buf[mark + 4] !== 0) {
|
| 1875 |
+
from = mark + 4;
|
| 1876 |
+
continue;
|
| 1877 |
+
}
|
| 1878 |
+
const count = buf.readUInt32BE(mark + 8);
|
| 1879 |
+
if (count < 2 || count > 8 || mark + 12 + count * 12 > buf.length) {
|
| 1880 |
+
from = mark + 4;
|
| 1881 |
+
continue;
|
| 1882 |
+
}
|
| 1883 |
+
const kind = handlerNear(buf, mark - 4 + size);
|
| 1884 |
+
const e0dur = buf.readUInt32BE(mark + 12);
|
| 1885 |
+
const e0mt = buf.readInt32BE(mark + 16);
|
| 1886 |
+
if (kind === "vide" && e0mt === -1 && e0dur > 0) {
|
| 1887 |
+
return e0dur / movieTs;
|
| 1888 |
+
}
|
| 1889 |
+
from = mark + 4;
|
| 1890 |
+
}
|
| 1891 |
+
return 0;
|
| 1892 |
+
}
|
| 1893 |
+
|
| 1894 |
+
function rememberElstLead(msgId, buf) {
|
| 1895 |
+
const id = String(msgId || "");
|
| 1896 |
+
if (!id || elstLeadSec.has(id)) return;
|
| 1897 |
+
const sec = videoEmptyEditSec(buf);
|
| 1898 |
+
if (sec > 0.05) elstLeadSec.set(id, sec);
|
| 1899 |
+
}
|
| 1900 |
+
|
| 1901 |
+
function patchAvSync(buf, fileOff, msgId, ua) {
|
| 1902 |
if (!buf || buf.length < 32 || fileOff > 8 * MB) return buf;
|
| 1903 |
+
if (geckoUA(ua)) return buf;
|
| 1904 |
const id = String(msgId || "");
|
| 1905 |
let state = avLead.get(id);
|
| 1906 |
if (!state) {
|
|
|
|
| 1939 |
const e1mt = out.readInt32BE(mark + 28);
|
| 1940 |
if (e1dur > 0 && e1mt >= 0) {
|
| 1941 |
if (out === buf) out = Buffer.from(buf);
|
| 1942 |
+
out.writeUInt32BE(2, mark + 8);
|
| 1943 |
out.writeUInt32BE((e0dur + e1dur) >>> 0, mark + 12);
|
| 1944 |
out.writeInt32BE(e1mt, mark + 16);
|
| 1945 |
+
out.writeUInt32BE(0, mark + 24);
|
| 1946 |
+
out.writeInt32BE(e1mt, mark + 28);
|
| 1947 |
state.delay = e0dur;
|
| 1948 |
+
if (state.movieTs > 0) elstLeadSec.set(id, e0dur / state.movieTs);
|
| 1949 |
}
|
| 1950 |
} else if (kind === "soun" && state.delay > 0 && e0mt === 0 && ts > 0 && e0dur > state.delay) {
|
| 1951 |
const skip = Math.round(state.delay * ts / state.movieTs);
|
|
|
|
| 1990 |
if (buf.length > left) buf = buf.subarray(0, left);
|
| 1991 |
if (!buf.length) continue;
|
| 1992 |
hotFill(msgId, at, buf, size);
|
| 1993 |
+
rememberElstLead(msgId, buf);
|
| 1994 |
+
const out = patchAvSync(buf, at, msgId, req.headers["user-agent"]);
|
| 1995 |
at += buf.length;
|
| 1996 |
const ok = res.write(out);
|
| 1997 |
if (!ok) await waitDrain(req, res);
|
|
|
|
| 2180 |
let pos = start;
|
| 2181 |
const prefix = hotPrefix(msgId, pos, end);
|
| 2182 |
if (prefix && prefix.length) {
|
| 2183 |
+
rememberElstLead(msgId, prefix);
|
| 2184 |
+
const patched = patchAvSync(prefix, pos, msgId, req.headers["user-agent"]);
|
| 2185 |
const ok = res.write(patched);
|
| 2186 |
lastByte = Date.now();
|
| 2187 |
pos += prefix.length;
|
|
|
|
| 2193 |
for await (const buf of gen) {
|
| 2194 |
if (stop()) break;
|
| 2195 |
hotFill(msgId, at, buf, rec.size);
|
| 2196 |
+
rememberElstLead(msgId, buf);
|
| 2197 |
+
const out = patchAvSync(buf, at, msgId, req.headers["user-agent"]);
|
| 2198 |
at += buf.length;
|
| 2199 |
const ok = res.write(out);
|
| 2200 |
lastByte = Date.now();
|
|
|
|
| 2249 |
const body = Object.assign({}, ghit.body);
|
| 2250 |
const cd = durCache.get(String(msgId));
|
| 2251 |
if (cd > 1) body.d = Math.floor(cd);
|
| 2252 |
+
const leadHit = elstLeadSec.get(String(msgId)) || 0;
|
| 2253 |
+
if (leadHit > 0.05) body.v = Math.round(leadHit * 1000) / 1000;
|
| 2254 |
return json(res, 200, body);
|
| 2255 |
}
|
| 2256 |
}
|
|
|
|
| 2301 |
const payload = { m: msgId, h: hash, u: uid || "anon", l: lvl, b: bot.idx, e: exp, s: rec.size, n: playMime, p: ipTag(ip), a: uaTag(req.headers["user-agent"]), d: durSec };
|
| 2302 |
const k = sealTicket(payload);
|
| 2303 |
const host = publicSpaceOrigin(req);
|
| 2304 |
+
if (!elstLeadSec.has(hid)) {
|
| 2305 |
+
const hh = hotMap.get(hid);
|
| 2306 |
+
if (hh && hh.head) rememberElstLead(msgId, hh.head);
|
| 2307 |
+
else if (hh && hh.tail) rememberElstLead(msgId, hh.tail);
|
| 2308 |
+
}
|
| 2309 |
+
const leadSec = elstLeadSec.get(hid) || 0;
|
| 2310 |
const body = {
|
| 2311 |
u: host + "/p/" + k + ".mp4",
|
| 2312 |
d: durSec,
|
|
|
|
| 2314 |
a: lvl,
|
| 2315 |
s: rec.size
|
| 2316 |
};
|
| 2317 |
+
if (leadSec > 0.05) body.v = Math.round(leadSec * 1000) / 1000;
|
| 2318 |
if (!rawAuth && lvl === "anonymous") {
|
| 2319 |
genAnonCache.set(path + "|" + hash + "|a|" + ipTag(ip) + "|" + uaTag(req.headers["user-agent"]), { body, exp: Date.now() + 180000, ts: Date.now() });
|
| 2320 |
}
|
|
|
|
| 2375 |
try { res.socket.setNoDelay(true); } catch { }
|
| 2376 |
}
|
| 2377 |
res.writeHead(206, headersHot);
|
| 2378 |
+
rememberElstLead(msgId, hit);
|
| 2379 |
+
res.end(patchAvSync(hit.subarray(0, needHot), rngHot.start, msgId, uaH));
|
| 2380 |
return;
|
| 2381 |
}
|
| 2382 |
let pooled = null;
|