Bladeren bron

Improve jpeg performance

Johannes Zellner 7 maanden geleden
bovenliggende
commit
305414bf37
4 gewijzigde bestanden met toevoegingen van 105 en 31 verwijderingen
  1. 29 8
      server/gstreamer.mjs
  2. 11 1
      server/index.mjs
  3. 34 12
      server/jpeg-framer.mjs
  4. 31 10
      viewer/index.html

+ 29 - 8
server/gstreamer.mjs

@@ -1,13 +1,34 @@
-import { spawn } from "node:child_process";
+import { spawn, execSync } from "node:child_process";
 import { createJpegFramer } from "./jpeg-framer.mjs";
 import { createJpegFramer } from "./jpeg-framer.mjs";
 
 
-/** @param {{ fps: number; quality: number }} opts */
-export function buildPipeline(source, { fps, quality }) {
-  return (
-    `${source} ! videoconvert ! videoscale ! video/x-raw,width=1280,height=720 ! ` +
-    `videorate ! video/x-raw,framerate=${fps}/1 ! jpegenc quality=${quality} ! ` +
-    `fdsink fd=1 sync=false`
-  );
+const HW_ENCODERS = ["vaapijpegenc"];
+
+let _jpegEncoder;
+
+export function detectJpegEncoder() {
+  if (_jpegEncoder !== undefined) return _jpegEncoder;
+  for (const enc of HW_ENCODERS) {
+    try {
+      execSync(`gst-inspect-1.0 ${enc}`, { stdio: "ignore" });
+      _jpegEncoder = enc;
+      return enc;
+    } catch { /* not available */ }
+  }
+  _jpegEncoder = "jpegenc";
+  return _jpegEncoder;
+}
+
+/** @param {{ fps: number; quality: number; width?: number; height?: number }} opts */
+export function buildPipeline(source, { fps, quality, width, height }) {
+  let pipeline = `${source} ! videoconvert`;
+  if (width && height) {
+    pipeline += ` ! videoscale ! video/x-raw,width=${width},height=${height}`;
+  }
+  pipeline += ` ! videorate ! video/x-raw,framerate=${fps}/1`;
+  const encoder = detectJpegEncoder();
+  pipeline += ` ! ${encoder} quality=${quality}`;
+  pipeline += ` ! fdsink fd=1 sync=false`;
+  return pipeline;
 }
 }
 
 
 /**
 /**

+ 11 - 1
server/index.mjs

@@ -24,6 +24,8 @@ function parseArgs(argv) {
     port: 9000,
     port: 9000,
     fps: 30,
     fps: 30,
     quality: 80,
     quality: 80,
+    width: 1280,
+    height: 720,
     source: null,
     source: null,
     verbose: false,
     verbose: false,
   };
   };
@@ -34,6 +36,8 @@ function parseArgs(argv) {
     else if (a === "--port") out.port = Number(argv[++i]);
     else if (a === "--port") out.port = Number(argv[++i]);
     else if (a === "--fps") out.fps = Number(argv[++i]);
     else if (a === "--fps") out.fps = Number(argv[++i]);
     else if (a === "--quality") out.quality = Number(argv[++i]);
     else if (a === "--quality") out.quality = Number(argv[++i]);
+    else if (a === "--width") out.width = Number(argv[++i]);
+    else if (a === "--height") out.height = Number(argv[++i]);
     else if (a === "--source") out.source = argv[++i];
     else if (a === "--source") out.source = argv[++i];
     else if (a === "--verbose" || a === "-v") out.verbose = true;
     else if (a === "--verbose" || a === "-v") out.verbose = true;
     else if (a === "--help" || a === "-h") {
     else if (a === "--help" || a === "-h") {
@@ -43,6 +47,8 @@ function parseArgs(argv) {
   --port N          Viewer (HTTP) and WebSocket on same port (default 9000)
   --port N          Viewer (HTTP) and WebSocket on same port (default 9000)
   --fps N           Target framerate (default 30)
   --fps N           Target framerate (default 30)
   --quality N       JPEG 1-100 (default 80)
   --quality N       JPEG 1-100 (default 80)
+  --width N         Scale output width (default 1280, 0 = source native)
+  --height N        Scale output height (default 720, 0 = source native)
   --source STR      Override GStreamer source element chain (before ! videoconvert)
   --source STR      Override GStreamer source element chain (before ! videoconvert)
   --verbose         Extra portal + pipeline debug (also NEBUCAST_VERBOSE=1)
   --verbose         Extra portal + pipeline debug (also NEBUCAST_VERBOSE=1)
 
 
@@ -150,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 > 8 * 1024 * 1024) continue;
+        if (ws.bufferedAmount > 128 * 1024) continue;
         try {
         try {
           ws.send(frame, { binary: true });
           ws.send(frame, { binary: true });
         } catch {
         } catch {
@@ -255,6 +261,8 @@ async function main() {
       const pipeline = buildPipeline(pipeSource, {
       const pipeline = buildPipeline(pipeSource, {
         fps: opts.fps,
         fps: opts.fps,
         quality: opts.quality,
         quality: opts.quality,
+        width: opts.width,
+        height: opts.height,
       });
       });
       if (opts.verbose) console.info("[gstreamer] pipeline:", pipeline);
       if (opts.verbose) console.info("[gstreamer] pipeline:", pipeline);
       const gst = startGstreamer({
       const gst = startGstreamer({
@@ -271,6 +279,8 @@ async function main() {
       const pipeline = buildPipeline(source, {
       const pipeline = buildPipeline(source, {
         fps: opts.fps,
         fps: opts.fps,
         quality: opts.quality,
         quality: opts.quality,
+        width: opts.width,
+        height: opts.height,
       });
       });
       if (opts.verbose) console.info("[gstreamer] pipeline:", pipeline);
       if (opts.verbose) console.info("[gstreamer] pipeline:", pipeline);
       const gst = startGstreamer({
       const gst = startGstreamer({

+ 34 - 12
server/jpeg-framer.mjs

@@ -3,29 +3,51 @@ const EOI = Buffer.from([0xff, 0xd9]);
 
 
 /**
 /**
  * Buffers stdout chunks and invokes onFrame with each complete JPEG (SOI … EOI).
  * Buffers stdout chunks and invokes onFrame with each complete JPEG (SOI … EOI).
+ * Defers concatenation until a scan for frame boundaries is needed,
+ * avoiding an O(buffered) copy on every incoming chunk.
  */
  */
 export function createJpegFramer(onFrame) {
 export function createJpegFramer(onFrame) {
-  let buf = Buffer.alloc(0);
+  let parts = [];
+  let len = 0;
+
+  function flatten() {
+    if (parts.length <= 1) return parts[0] || Buffer.alloc(0);
+    const buf = Buffer.concat(parts, len);
+    parts = [buf];
+    return buf;
+  }
 
 
   return {
   return {
     push(chunk) {
     push(chunk) {
       if (!chunk.length) return;
       if (!chunk.length) return;
-      buf = Buffer.concat([buf, chunk]);
+      parts.push(chunk);
+      len += chunk.length;
+
+      while (len >= 4) {
+        let buf = flatten();
 
 
-      while (buf.length >= 4) {
-        const start = buf.indexOf(SOI);
-        if (start === -1) {
-          buf = buf.length >= 1 ? buf.subarray(buf.length - 1) : Buffer.alloc(0);
+        const soi = buf.indexOf(SOI);
+        if (soi === -1) {
+          buf = buf.subarray(buf.length - 1);
+          parts = [buf];
+          len = buf.length;
           break;
           break;
         }
         }
-        if (start > 0) buf = buf.subarray(start);
+        if (soi > 0) {
+          buf = buf.subarray(soi);
+          parts = [buf];
+          len = buf.length;
+        }
+
+        const eoi = buf.indexOf(EOI, 2);
+        if (eoi === -1) break;
 
 
-        const end = buf.indexOf(EOI, 2);
-        if (end === -1) break;
+        const frameEnd = eoi + 2;
+        onFrame(buf.subarray(0, frameEnd));
 
 
-        const frame = buf.subarray(0, end + 2);
-        buf = buf.subarray(end + 2);
-        onFrame(Buffer.from(frame));
+        buf = buf.subarray(frameEnd);
+        parts = buf.length ? [buf] : [];
+        len = buf.length;
       }
       }
     },
     },
   };
   };

+ 31 - 10
viewer/index.html

@@ -52,7 +52,6 @@
   #frame{
   #frame{
     max-width:100%;max-height:100%;border-radius:8px;
     max-width:100%;max-height:100%;border-radius:8px;
     background:#000;box-shadow:0 4px 24px rgba(0,0,0,.5);
     background:#000;box-shadow:0 4px 24px rgba(0,0,0,.5);
-    image-rendering:auto;
   }
   }
 
 
   .stats{
   .stats{
@@ -74,7 +73,7 @@
   </div>
   </div>
 
 
   <div class="video-wrap">
   <div class="video-wrap">
-    <img id="frame" alt="">
+    <canvas id="frame" width="1280" height="720"></canvas>
   </div>
   </div>
 
 
   <div class="stats">
   <div class="stats">
@@ -86,7 +85,8 @@
 
 
 <script>
 <script>
 (function () {
 (function () {
-  var frame      = document.getElementById("frame");
+  var canvas     = document.getElementById("frame");
+  var ctx        = canvas.getContext("2d");
   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");
@@ -99,7 +99,8 @@
   var totalBytes = 0;
   var totalBytes = 0;
   var fpsCounter = 0;
   var fpsCounter = 0;
   var fpsTimer = null;
   var fpsTimer = null;
-  var prevUrl = null;
+  var pendingData = null;
+  var decoding = false;
 
 
   function formatBytes(n) {
   function formatBytes(n) {
     if (n < 1024) return n + " B";
     if (n < 1024) return n + " B";
@@ -113,6 +114,26 @@
     return scheme + "//" + location.host + "/ws";
     return scheme + "//" + location.host + "/ws";
   }
   }
 
 
+  function decodeLatest() {
+    if (!pendingData || decoding) return;
+    var data = pendingData;
+    pendingData = null;
+    decoding = true;
+    createImageBitmap(new Blob([data], { type: "image/jpeg" })).then(function (bmp) {
+      if (canvas.width !== bmp.width || canvas.height !== bmp.height) {
+        canvas.width = bmp.width;
+        canvas.height = bmp.height;
+      }
+      ctx.drawImage(bmp, 0, 0);
+      bmp.close();
+      decoding = false;
+      decodeLatest();
+    }, function () {
+      decoding = false;
+      decodeLatest();
+    });
+  }
+
   function connect() {
   function connect() {
     if (ws) disconnect();
     if (ws) disconnect();
 
 
@@ -131,11 +152,8 @@
     };
     };
 
 
     ws.onmessage = function (ev) {
     ws.onmessage = function (ev) {
-      var blob = new Blob([ev.data], { type: "image/jpeg" });
-      var url = URL.createObjectURL(blob);
-      if (prevUrl) URL.revokeObjectURL(prevUrl);
-      prevUrl = url;
-      frame.src = url;
+      pendingData = ev.data;
+      decodeLatest();
 
 
       totalFrames++;
       totalFrames++;
       fpsCounter++;
       fpsCounter++;
@@ -151,7 +169,8 @@
   function disconnect() {
   function disconnect() {
     if (ws) { ws.close(); ws = null; }
     if (ws) { ws.close(); ws = null; }
     if (fpsTimer) { clearInterval(fpsTimer); fpsTimer = null; }
     if (fpsTimer) { clearInterval(fpsTimer); fpsTimer = null; }
-    if (prevUrl) { URL.revokeObjectURL(prevUrl); prevUrl = null; }
+    pendingData = null;
+    ctx.clearRect(0, 0, canvas.width, canvas.height);
     setDisconnected();
     setDisconnected();
   }
   }
 
 
@@ -166,6 +185,8 @@
     if (ws && ws.readyState === WebSocket.OPEN) disconnect();
     if (ws && ws.readyState === WebSocket.OPEN) disconnect();
     else connect();
     else connect();
   });
   });
+
+  connect();
 })();
 })();
 </script>
 </script>
 </body>
 </body>