Parcourir la source

Reduce delays

Johannes Zellner il y a 7 mois
Parent
commit
6afe22d9cb
3 fichiers modifiés avec 11 ajouts et 8 suppressions
  1. 2 1
      server/gstreamer.mjs
  2. 3 1
      server/index.mjs
  3. 6 6
      viewer/index.html

+ 2 - 1
server/gstreamer.mjs

@@ -24,7 +24,8 @@ export function buildPipeline(source, { fps, quality, width, height }) {
   if (width && height) {
   if (width && height) {
     pipeline += ` ! videoscale ! video/x-raw,width=${width},height=${height}`;
     pipeline += ` ! videoscale ! video/x-raw,width=${width},height=${height}`;
   }
   }
-  pipeline += ` ! videorate ! video/x-raw,framerate=${fps}/1`;
+  pipeline += ` ! videorate drop-only=true max-rate=${fps}`;
+  pipeline += ` ! queue max-size-buffers=1 max-size-bytes=0 max-size-time=0 leaky=downstream`;
   const encoder = detectJpegEncoder();
   const encoder = detectJpegEncoder();
   pipeline += ` ! ${encoder} quality=${quality}`;
   pipeline += ` ! ${encoder} quality=${quality}`;
   pipeline += ` ! fdsink fd=1 sync=false`;
   pipeline += ` ! fdsink fd=1 sync=false`;

+ 3 - 1
server/index.mjs

@@ -156,7 +156,7 @@ async function main() {
     function broadcastFrame(frame) {
     function broadcastFrame(frame) {
       for (const ws of clients) {
       for (const ws of clients) {
         if (ws.readyState !== ws.OPEN) continue;
         if (ws.readyState !== ws.OPEN) continue;
-        if (ws.bufferedAmount > 128 * 1024) continue;
+        if (ws.bufferedAmount > 64 * 1024) continue;
         try {
         try {
           ws.send(frame, { binary: true });
           ws.send(frame, { binary: true });
         } catch {
         } catch {
@@ -186,6 +186,8 @@ async function main() {
         res.end();
         res.end();
       });
       });
 
 
+      httpServer.on("connection", (socket) => socket.setNoDelay(true));
+
       wss = new WebSocketServer({
       wss = new WebSocketServer({
         server: httpServer,
         server: httpServer,
         path: "/ws",
         path: "/ws",

+ 6 - 6
viewer/index.html

@@ -86,7 +86,7 @@
 <script>
 <script>
 (function () {
 (function () {
   var canvas     = document.getElementById("frame");
   var canvas     = document.getElementById("frame");
-  var ctx        = canvas.getContext("2d");
+  var ctx        = canvas.getContext("2d", { alpha: false, desynchronized: true });
   var btnConn    = document.getElementById("btn-connect");
   var btnConn    = document.getElementById("btn-connect");
   var statusEl   = document.getElementById("status");
   var statusEl   = document.getElementById("status");
   var statusTxt  = document.getElementById("status-text");
   var statusTxt  = document.getElementById("status-text");
@@ -119,7 +119,7 @@
     var data = pendingData;
     var data = pendingData;
     pendingData = null;
     pendingData = null;
     decoding = true;
     decoding = true;
-    createImageBitmap(new Blob([data], { type: "image/jpeg" })).then(function (bmp) {
+    createImageBitmap(data).then(function (bmp) {
       if (canvas.width !== bmp.width || canvas.height !== bmp.height) {
       if (canvas.width !== bmp.width || canvas.height !== bmp.height) {
         canvas.width = bmp.width;
         canvas.width = bmp.width;
         canvas.height = bmp.height;
         canvas.height = bmp.height;
@@ -138,7 +138,7 @@
     if (ws) disconnect();
     if (ws) disconnect();
 
 
     ws = new WebSocket(wsUrlFromLocation());
     ws = new WebSocket(wsUrlFromLocation());
-    ws.binaryType = "arraybuffer";
+    ws.binaryType = "blob";
 
 
     ws.onopen = function () {
     ws.onopen = function () {
       statusEl.className = "live";
       statusEl.className = "live";
@@ -147,6 +147,8 @@
       btnConn.className = "disconnect";
       btnConn.className = "disconnect";
       fpsTimer = setInterval(function () {
       fpsTimer = setInterval(function () {
         statFps.textContent = fpsCounter;
         statFps.textContent = fpsCounter;
+        statFrames.textContent = totalFrames;
+        statBytes.textContent = formatBytes(totalBytes);
         fpsCounter = 0;
         fpsCounter = 0;
       }, 1000);
       }, 1000);
     };
     };
@@ -157,9 +159,7 @@
 
 
       totalFrames++;
       totalFrames++;
       fpsCounter++;
       fpsCounter++;
-      totalBytes += ev.data.byteLength;
-      statFrames.textContent = totalFrames;
-      statBytes.textContent = formatBytes(totalBytes);
+      totalBytes += ev.data.size;
     };
     };
 
 
     ws.onclose = function () { setDisconnected(); };
     ws.onclose = function () { setDisconnected(); };