Spaces:
Running
Running
JOHN SILLVESTOR commited on
Align Antz audio with the picture
Browse files
server.js
CHANGED
|
@@ -1830,6 +1830,86 @@ async function rangeSlicesReady(msgId, start, end) {
|
|
| 1830 |
return true;
|
| 1831 |
}
|
| 1832 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1833 |
async function pipeDiskRange(msgId, start, end, req, res, headers, size) {
|
| 1834 |
const t0 = Date.now();
|
| 1835 |
let clientClosed = false;
|
|
@@ -1860,8 +1940,9 @@ async function pipeDiskRange(msgId, start, end, req, res, headers, size) {
|
|
| 1860 |
if (buf.length > left) buf = buf.subarray(0, left);
|
| 1861 |
if (!buf.length) continue;
|
| 1862 |
hotFill(msgId, at, buf, size);
|
|
|
|
| 1863 |
at += buf.length;
|
| 1864 |
-
const ok = res.write(
|
| 1865 |
if (!ok) await waitDrain(req, res);
|
| 1866 |
}
|
| 1867 |
return at > end;
|
|
@@ -2048,7 +2129,8 @@ async function pipeRange(bot, rec, start, end, req, res, status, headers, msgId)
|
|
| 2048 |
let pos = start;
|
| 2049 |
const prefix = hotPrefix(msgId, pos, end);
|
| 2050 |
if (prefix && prefix.length) {
|
| 2051 |
-
const
|
|
|
|
| 2052 |
lastByte = Date.now();
|
| 2053 |
pos += prefix.length;
|
| 2054 |
if (!ok) await waitDrain(req, res);
|
|
@@ -2059,8 +2141,9 @@ async function pipeRange(bot, rec, start, end, req, res, status, headers, msgId)
|
|
| 2059 |
for await (const buf of gen) {
|
| 2060 |
if (stop()) break;
|
| 2061 |
hotFill(msgId, at, buf, rec.size);
|
|
|
|
| 2062 |
at += buf.length;
|
| 2063 |
-
const ok = res.write(
|
| 2064 |
lastByte = Date.now();
|
| 2065 |
if (!ok) await waitDrain(req, res);
|
| 2066 |
}
|
|
@@ -2230,7 +2313,7 @@ async function handleStream(req, res, url, msgId, kHex, ticketObj) {
|
|
| 2230 |
try { res.socket.setNoDelay(true); } catch { }
|
| 2231 |
}
|
| 2232 |
res.writeHead(206, headersHot);
|
| 2233 |
-
res.end(hit.subarray(0, needHot));
|
| 2234 |
return;
|
| 2235 |
}
|
| 2236 |
let pooled = null;
|
|
|
|
| 1830 |
return true;
|
| 1831 |
}
|
| 1832 |
|
| 1833 |
+
const avLead = new Map();
|
| 1834 |
+
|
| 1835 |
+
function handlerNear(buf, from) {
|
| 1836 |
+
const end = Math.min(buf.length - 4, from + 180);
|
| 1837 |
+
for (let i = from; i < end; i++) {
|
| 1838 |
+
if (buf.toString("ascii", i, i + 4) !== "hdlr" || i + 16 > buf.length) continue;
|
| 1839 |
+
const h = buf.toString("ascii", i + 12, i + 16);
|
| 1840 |
+
if (h === "vide" || h === "soun") return h;
|
| 1841 |
+
}
|
| 1842 |
+
return "";
|
| 1843 |
+
}
|
| 1844 |
+
|
| 1845 |
+
function timescaleNear(buf, from) {
|
| 1846 |
+
const end = Math.min(buf.length - 8, from + 180);
|
| 1847 |
+
for (let i = from; i < end; i++) {
|
| 1848 |
+
if (buf.toString("ascii", i, i + 4) !== "mdhd" || buf[i + 4] !== 0 || i + 20 > buf.length) continue;
|
| 1849 |
+
const ts = buf.readUInt32BE(i + 16);
|
| 1850 |
+
if (ts > 1 && ts < 200000) return ts;
|
| 1851 |
+
}
|
| 1852 |
+
return 0;
|
| 1853 |
+
}
|
| 1854 |
+
|
| 1855 |
+
function patchAvSync(buf, fileOff, msgId) {
|
| 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) {
|
| 1860 |
+
state = { delay: 0, movieTs: 1000 };
|
| 1861 |
+
avLead.set(id, state);
|
| 1862 |
+
}
|
| 1863 |
+
if (avLead.size > 4000) avLead.clear();
|
| 1864 |
+
if (fileOff === 0) {
|
| 1865 |
+
const mv = buf.indexOf("mvhd");
|
| 1866 |
+
if (mv > 0 && mv + 24 < buf.length && buf[mv + 4] === 0) {
|
| 1867 |
+
const ts = buf.readUInt32BE(mv + 16);
|
| 1868 |
+
if (ts > 0 && ts < 1000000) state.movieTs = ts;
|
| 1869 |
+
}
|
| 1870 |
+
}
|
| 1871 |
+
let out = buf;
|
| 1872 |
+
let from = 0;
|
| 1873 |
+
while (from + 12 < out.length) {
|
| 1874 |
+
const mark = out.indexOf("elst", from);
|
| 1875 |
+
if (mark < 4) break;
|
| 1876 |
+
const size = out.readUInt32BE(mark - 4);
|
| 1877 |
+
if (size < 28 || size > 512 || mark - 4 + size > out.length || out[mark + 4] !== 0) {
|
| 1878 |
+
from = mark + 4;
|
| 1879 |
+
continue;
|
| 1880 |
+
}
|
| 1881 |
+
const count = out.readUInt32BE(mark + 8);
|
| 1882 |
+
if (count < 1 || count > 8 || mark + 12 + count * 12 > out.length) {
|
| 1883 |
+
from = mark + 4;
|
| 1884 |
+
continue;
|
| 1885 |
+
}
|
| 1886 |
+
const kind = handlerNear(out, mark - 4 + size);
|
| 1887 |
+
const ts = timescaleNear(out, mark - 4 + size);
|
| 1888 |
+
const e0dur = out.readUInt32BE(mark + 12);
|
| 1889 |
+
const e0mt = out.readInt32BE(mark + 16);
|
| 1890 |
+
if (kind === "vide" && count >= 2 && e0mt === -1 && e0dur > 0) {
|
| 1891 |
+
const e1dur = out.readUInt32BE(mark + 24);
|
| 1892 |
+
const e1mt = out.readInt32BE(mark + 28);
|
| 1893 |
+
if (e1dur > 0 && e1mt >= 0) {
|
| 1894 |
+
if (out === buf) out = Buffer.from(buf);
|
| 1895 |
+
out.writeUInt32BE(1, mark + 8);
|
| 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);
|
| 1902 |
+
if (skip > 0) {
|
| 1903 |
+
if (out === buf) out = Buffer.from(buf);
|
| 1904 |
+
out.writeUInt32BE(e0dur - state.delay, mark + 12);
|
| 1905 |
+
out.writeInt32BE(skip, mark + 16);
|
| 1906 |
+
}
|
| 1907 |
+
}
|
| 1908 |
+
from = mark + 4;
|
| 1909 |
+
}
|
| 1910 |
+
return out;
|
| 1911 |
+
}
|
| 1912 |
+
|
| 1913 |
async function pipeDiskRange(msgId, start, end, req, res, headers, size) {
|
| 1914 |
const t0 = Date.now();
|
| 1915 |
let clientClosed = false;
|
|
|
|
| 1940 |
if (buf.length > left) buf = buf.subarray(0, left);
|
| 1941 |
if (!buf.length) continue;
|
| 1942 |
hotFill(msgId, at, buf, size);
|
| 1943 |
+
const out = patchAvSync(buf, at, msgId);
|
| 1944 |
at += buf.length;
|
| 1945 |
+
const ok = res.write(out);
|
| 1946 |
if (!ok) await waitDrain(req, res);
|
| 1947 |
}
|
| 1948 |
return at > end;
|
|
|
|
| 2129 |
let pos = start;
|
| 2130 |
const prefix = hotPrefix(msgId, pos, end);
|
| 2131 |
if (prefix && prefix.length) {
|
| 2132 |
+
const patched = patchAvSync(prefix, pos, msgId);
|
| 2133 |
+
const ok = res.write(patched);
|
| 2134 |
lastByte = Date.now();
|
| 2135 |
pos += prefix.length;
|
| 2136 |
if (!ok) await waitDrain(req, res);
|
|
|
|
| 2141 |
for await (const buf of gen) {
|
| 2142 |
if (stop()) break;
|
| 2143 |
hotFill(msgId, at, buf, rec.size);
|
| 2144 |
+
const out = patchAvSync(buf, at, msgId);
|
| 2145 |
at += buf.length;
|
| 2146 |
+
const ok = res.write(out);
|
| 2147 |
lastByte = Date.now();
|
| 2148 |
if (!ok) await waitDrain(req, res);
|
| 2149 |
}
|
|
|
|
| 2313 |
try { res.socket.setNoDelay(true); } catch { }
|
| 2314 |
}
|
| 2315 |
res.writeHead(206, headersHot);
|
| 2316 |
+
res.end(patchAvSync(hit.subarray(0, needHot), rngHot.start, msgId));
|
| 2317 |
return;
|
| 2318 |
}
|
| 2319 |
let pooled = null;
|