JOHN SILLVESTOR commited on
Commit
de99b48
·
verified ·
1 Parent(s): e34f2a7

Use smaller video slices so playback starts sooner

Browse files
Files changed (1) hide show
  1. server.js +72 -13
server.js CHANGED
@@ -36,7 +36,7 @@ function cgroupLimitBytes() {
36
  }
37
  const CGROUP_BYTES = cgroupLimitBytes();
38
  const RAM_BYTES = CGROUP_BYTES || os.totalmem() || (16 * 1024 * MB);
39
- const CHUNK_SIZE = 1024 * 1024;
40
  const HOT_HEAD = 24 * MB;
41
  const HOT_TAIL = 8 * MB;
42
  const STREAM_WINDOW = 8 * MB;
@@ -1789,13 +1789,19 @@ function kickWarmAhead(bot, rec, msgId, fromOff, fileSize, wantBytes) {
1789
  })();
1790
  }
1791
 
1792
- async function fetchSliceChunk(bot, rec, msgId, aligned) {
1793
  const disk = await readDiskSlice(msgId, aligned);
1794
- if (disk && disk.length) return disk;
 
 
 
1795
  const flightKey = String(msgId) + ":" + String(aligned);
1796
  return chunkFlight.do(flightKey, async () => {
1797
  const recheck = await readDiskSlice(msgId, aligned);
1798
- if (recheck && recheck.length) return recheck;
 
 
 
1799
  let downloadBot = bot;
1800
  if (!downloadBot || !botUsable(downloadBot) || (downloadBot.tgLoad || 0) > 0) {
1801
  downloadBot = pickBot(undefined, undefined, msgId) || downloadBot;
@@ -1810,8 +1816,13 @@ async function fetchSliceChunk(bot, rec, msgId, aligned) {
1810
  dcId: rec.dcId
1811
  });
1812
  const parts = [];
 
1813
  for await (const chunk of it) {
1814
- parts.push(Buffer.from(chunk));
 
 
 
 
1815
  }
1816
  const full = Buffer.concat(parts);
1817
  if (full.length) {
@@ -1922,25 +1933,73 @@ async function pipeRange(bot, rec, start, end, req, res, status, headers, msgId)
1922
  }
1923
  if (pipeDead(req, res)) return;
1924
  const primedPrefix = hotPrefix(msgId, start, end);
 
 
1925
  if (!(primedPrefix && primedPrefix.length)) {
1926
- const primed = await fetchSliceChunk(bot, rec, msgId, Math.floor(start / CHUNK_SIZE) * CHUNK_SIZE);
1927
- if (!primed || !primed.length) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1928
  if (!res.headersSent) {
1929
  res.writeHead(502);
1930
  res.end();
1931
  }
1932
  return;
1933
  }
 
 
 
 
1934
  }
1935
- kickWarmAhead(bot, rec, msgId, start, rec.size || (end + 1), end - start + 1);
1936
  if (pipeDead(req, res)) return;
1937
- res.writeHead(status, headers);
1938
- if (typeof res.flushHeaders === "function") res.flushHeaders();
 
 
1939
  let lastByte = Date.now();
1940
  const budget = pipeBudget(start, end);
1941
  const stop = () => clientClosed || pipeDead(req, res) || Date.now() - lastByte > 22000 || Date.now() - t0 > budget;
1942
- let pos = start;
1943
- const prefix = hotPrefix(msgId, pos, end);
1944
  if (prefix && prefix.length) {
1945
  rememberElstLead(msgId, prefix);
1946
  const patched = patchAvSync(prefix, pos, msgId, req.headers["user-agent"]);
@@ -1998,7 +2057,7 @@ async function handleGenerate(req, res, url) {
1998
  if (raw && raw.enc) {
1999
  const oHeader = req.headers.origin || req.headers.referer || "";
2000
  let dom = "dublk2000.blogspot.com";
2001
- try { if (oHeader) dom = new URL(oHeader.includes("://") ? oHeader : "https://" + oHeader).hostname; } catch {}
2002
  const decReq = (enc, d) => {
2003
  try {
2004
  const key = Buffer.from(d, "utf8");
 
36
  }
37
  const CGROUP_BYTES = cgroupLimitBytes();
38
  const RAM_BYTES = CGROUP_BYTES || os.totalmem() || (16 * 1024 * MB);
39
+ const CHUNK_SIZE = 256 * 1024;
40
  const HOT_HEAD = 24 * MB;
41
  const HOT_TAIL = 8 * MB;
42
  const STREAM_WINDOW = 8 * MB;
 
1789
  })();
1790
  }
1791
 
1792
+ async function fetchSliceChunk(bot, rec, msgId, aligned, onPiece) {
1793
  const disk = await readDiskSlice(msgId, aligned);
1794
+ if (disk && disk.length) {
1795
+ if (onPiece) onPiece(disk, aligned);
1796
+ return disk;
1797
+ }
1798
  const flightKey = String(msgId) + ":" + String(aligned);
1799
  return chunkFlight.do(flightKey, async () => {
1800
  const recheck = await readDiskSlice(msgId, aligned);
1801
+ if (recheck && recheck.length) {
1802
+ if (onPiece) onPiece(recheck, aligned);
1803
+ return recheck;
1804
+ }
1805
  let downloadBot = bot;
1806
  if (!downloadBot || !botUsable(downloadBot) || (downloadBot.tgLoad || 0) > 0) {
1807
  downloadBot = pickBot(undefined, undefined, msgId) || downloadBot;
 
1816
  dcId: rec.dcId
1817
  });
1818
  const parts = [];
1819
+ let fileOff = aligned;
1820
  for await (const chunk of it) {
1821
+ const b = Buffer.from(chunk);
1822
+ if (!b.length) continue;
1823
+ parts.push(b);
1824
+ if (onPiece) onPiece(b, fileOff);
1825
+ fileOff += b.length;
1826
  }
1827
  const full = Buffer.concat(parts);
1828
  if (full.length) {
 
1933
  }
1934
  if (pipeDead(req, res)) return;
1935
  const primedPrefix = hotPrefix(msgId, start, end);
1936
+ let streamedFirst = false;
1937
+ let streamedTo = start;
1938
  if (!(primedPrefix && primedPrefix.length)) {
1939
+ const aligned = Math.floor(start / CHUNK_SIZE) * CHUNK_SIZE;
1940
+ let opened = false;
1941
+ const primedP = fetchSliceChunk(bot, rec, msgId, aligned, (piece, off) => {
1942
+ if (!piece || !piece.length || clientClosed || pipeDead(req, res)) return;
1943
+ let buf = piece;
1944
+ let at = off;
1945
+ if (at < start) {
1946
+ const skip = start - at;
1947
+ if (skip >= buf.length) return;
1948
+ buf = buf.subarray(skip);
1949
+ at = start;
1950
+ }
1951
+ if (at > end) return;
1952
+ const left = end - at + 1;
1953
+ if (buf.length > left) buf = buf.subarray(0, left);
1954
+ if (!buf.length || res.writableEnded) return;
1955
+ if (!opened) {
1956
+ res.writeHead(status, headers);
1957
+ if (typeof res.flushHeaders === "function") res.flushHeaders();
1958
+ opened = true;
1959
+ streamedFirst = true;
1960
+ }
1961
+ rememberElstLead(msgId, buf);
1962
+ const out = patchAvSync(buf, at, msgId, req.headers["user-agent"]);
1963
+ res.write(out);
1964
+ streamedTo = at + buf.length;
1965
+ });
1966
+ const ready = new Promise((resolve) => {
1967
+ const timer = setInterval(() => {
1968
+ if (streamedTo > end || clientClosed || pipeDead(req, res)) {
1969
+ clearInterval(timer);
1970
+ resolve();
1971
+ }
1972
+ }, 20);
1973
+ primedP.finally(() => {
1974
+ clearInterval(timer);
1975
+ resolve();
1976
+ });
1977
+ });
1978
+ await ready;
1979
+ const primed = opened ? null : await primedP.catch(() => null);
1980
+ if (!opened && (!primed || !primed.length)) {
1981
  if (!res.headersSent) {
1982
  res.writeHead(502);
1983
  res.end();
1984
  }
1985
  return;
1986
  }
1987
+ if (!opened) {
1988
+ res.writeHead(status, headers);
1989
+ if (typeof res.flushHeaders === "function") res.flushHeaders();
1990
+ }
1991
  }
1992
+ kickWarmAhead(bot, rec, msgId, streamedFirst ? streamedTo : start, rec.size || (end + 1), end - start + 1);
1993
  if (pipeDead(req, res)) return;
1994
+ if (!res.headersSent) {
1995
+ res.writeHead(status, headers);
1996
+ if (typeof res.flushHeaders === "function") res.flushHeaders();
1997
+ }
1998
  let lastByte = Date.now();
1999
  const budget = pipeBudget(start, end);
2000
  const stop = () => clientClosed || pipeDead(req, res) || Date.now() - lastByte > 22000 || Date.now() - t0 > budget;
2001
+ let pos = streamedFirst ? streamedTo : start;
2002
+ const prefix = streamedFirst ? null : hotPrefix(msgId, pos, end);
2003
  if (prefix && prefix.length) {
2004
  rememberElstLead(msgId, prefix);
2005
  const patched = patchAvSync(prefix, pos, msgId, req.headers["user-agent"]);
 
2057
  if (raw && raw.enc) {
2058
  const oHeader = req.headers.origin || req.headers.referer || "";
2059
  let dom = "dublk2000.blogspot.com";
2060
+ try { if (oHeader) dom = new URL(oHeader.includes("://") ? oHeader : "https://" + oHeader).hostname; } catch { }
2061
  const decReq = (enc, d) => {
2062
  try {
2063
  const key = Buffer.from(d, "utf8");