perf(frontend): 工作台预览改用 canvas 直绘帧,不再每帧置换 object URL
每次帧都 new Blob + createObjectURL 再塞给 img,连续播放时 URL 抖动、内存回收压力大。 改为把 WS 收到的 JPEG/PNG 帧直接画到磁贴 canvas(utils/frameCanvas 抽公共绘制器), 画布常驻只重绘,有无画面靠提示层说明。 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
e8923dbe56
commit
3469b6fcb3
@ -11,10 +11,11 @@
|
||||
*
|
||||
* 保存走 `PUT /api/web/workstations/:id/cameras/:cameraId/layout`。
|
||||
*/
|
||||
import { computed, onBeforeUnmount, onMounted, reactive, ref, watch } from 'vue';
|
||||
import { computed, onBeforeUnmount, onMounted, reactive, ref, watch, type ComponentPublicInstance } from 'vue';
|
||||
import { updateWorkstationCameraLayout } from '@/api/videoApi';
|
||||
import { fetchNvrCredential } from '@/api/videoApi';
|
||||
import { localWs } from '@/api/localWs';
|
||||
import { createFramePainter, type FramePainter } from '@/utils/frameCanvas';
|
||||
import LocalServiceGate from '@/components/LocalServiceGate.vue';
|
||||
import type { WorkstationCameraLink, WorkstationDetail } from '@/api/video';
|
||||
|
||||
@ -53,8 +54,27 @@ const error = ref('');
|
||||
/** 连不上本机软件 → 弹统一提示窗 */
|
||||
const gateOpen = ref(false);
|
||||
|
||||
/** 预览会话:cameraId → { sid, src, stop, url }(帧走 WS,object URL 每次置换) */
|
||||
const previews = reactive<Record<number, { sid: string; src: string; stop: () => void; url: string }>>({});
|
||||
/** 预览会话:cameraId → { sid, stop }(帧走 WS,直接画到该磁贴的 canvas) */
|
||||
const previews = reactive<Record<number, { sid: string; stop: () => void }>>({});
|
||||
/** 磁贴画布 / 帧绘制器:放普通 Map,DOM 与 Blob 都不该被响应式代理包一层 */
|
||||
const tileCanvases = new Map<number, HTMLCanvasElement>();
|
||||
const tilePainters = new Map<number, FramePainter>();
|
||||
|
||||
/** 磁贴画布的 v-ref 回调:挂上时建绘制器,卸载时销毁 */
|
||||
function bindTileCanvas(cameraId: number, el: Element | ComponentPublicInstance | null) {
|
||||
if (el instanceof HTMLCanvasElement) {
|
||||
tileCanvases.set(cameraId, el);
|
||||
if (!tilePainters.has(cameraId)) {
|
||||
tilePainters.set(cameraId, createFramePainter(() => tileCanvases.get(cameraId) || null, 'cover'));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
tileCanvases.delete(cameraId);
|
||||
tilePainters.get(cameraId)?.dispose();
|
||||
tilePainters.delete(cameraId);
|
||||
}
|
||||
|
||||
const credCache = new Map<number, { host: string; port: number; username: string; password: string }>();
|
||||
|
||||
const canvasW = computed(() => Number(props.canvasWidth) || 1920);
|
||||
@ -156,33 +176,17 @@ async function syncPreviews() {
|
||||
name: tile.name,
|
||||
});
|
||||
|
||||
const state: { sid: string; src: string; stop: () => void; url: string } = {
|
||||
const state: { sid: string; stop: () => void } = {
|
||||
sid: info.sid,
|
||||
src: '',
|
||||
stop: () => {},
|
||||
url: '',
|
||||
};
|
||||
|
||||
// 先挂进 previews,拿到 **reactive 代理**,后续帧只往代理上写。
|
||||
// ⚠️ 别再写本地对象:同一个 url 先赋给本地 state.src 再赋给代理,Vue 的值比较
|
||||
// 会判为“没变化”→ 不触发重渲染,画面就一直空白(踩过)。
|
||||
// 挂进 previews 让模板渲染出这块 canvas(绘制器由 v-ref 回调建好)
|
||||
previews[tile.cameraId] = state;
|
||||
const live = previews[tile.cameraId];
|
||||
|
||||
state.stop = localWs.onFrame(info.sid, blob => {
|
||||
const url = URL.createObjectURL(blob);
|
||||
const old = live.url;
|
||||
|
||||
if (!previews[tile.cameraId]) {
|
||||
// 该磁贴已经被删掉(改布局/切码流)→ 丢弃这帧
|
||||
URL.revokeObjectURL(url);
|
||||
return;
|
||||
}
|
||||
|
||||
live.url = url;
|
||||
live.src = url;
|
||||
|
||||
if (old) window.setTimeout(() => URL.revokeObjectURL(old), 1200);
|
||||
// 磁贴已被删掉(改布局/切码流)或画布还没挂上 → 丢弃这帧,下一帧再画
|
||||
tilePainters.get(tile.cameraId)?.push(blob);
|
||||
});
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : '预览失败';
|
||||
@ -204,7 +208,10 @@ async function stopPreview(cameraId: number) {
|
||||
delete previews[cameraId];
|
||||
item.stop();
|
||||
void localWs.stopPreview(item.sid);
|
||||
if (item.url) URL.revokeObjectURL(item.url);
|
||||
|
||||
// 画布随 v-if 卸载(v-ref 回调里也会清理,这里先停绘制,避免卸载瞬间还在画)
|
||||
tilePainters.get(cameraId)?.dispose();
|
||||
tilePainters.delete(cameraId);
|
||||
}
|
||||
|
||||
function releaseAll() {
|
||||
@ -367,12 +374,12 @@ const selected = computed(() => (selectedId.value ? tileById(selectedId.value) :
|
||||
}"
|
||||
@pointerdown="onPointerDown($event, tile)"
|
||||
>
|
||||
<div v-if="!previews[tile.cameraId]?.src" class="tile-fallback">{{ tile.name }}<br /><small>无画面(摄像头可能离线)</small></div>
|
||||
<img
|
||||
<div class="tile-fallback">{{ tile.name }}<br /><small>无画面(摄像头可能离线)</small></div>
|
||||
<canvas
|
||||
v-if="previews[tile.cameraId]"
|
||||
:src="previews[tile.cameraId].src"
|
||||
:alt="tile.name"
|
||||
draggable="false"
|
||||
:ref="el => bindTileCanvas(tile.cameraId, el)"
|
||||
class="tile-frame"
|
||||
:aria-label="tile.name"
|
||||
/>
|
||||
|
||||
<span class="tile-label">
|
||||
@ -472,16 +479,15 @@ const selected = computed(() => (selectedId.value ? tileById(selectedId.value) :
|
||||
box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.35);
|
||||
}
|
||||
|
||||
.tile img {
|
||||
.tile-frame {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
display: block;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
/* 图片没帧时(摄像头离线)显示在下方,有画面就被 img 盖住 */
|
||||
/* 没帧时(摄像头离线)露出来,有画面就被 canvas 盖住(canvas 未绘制时是透明的) */
|
||||
.tile-fallback {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
|
||||
162
frontend/src/utils/frameCanvas.ts
Normal file
162
frontend/src/utils/frameCanvas.ts
Normal file
@ -0,0 +1,162 @@
|
||||
/**
|
||||
* 把本机软件推来的 JPEG 帧画到 <canvas> 上
|
||||
*
|
||||
* 帧是**完整 JPEG**(WS 二进制:sid 长度 + sid + JPEG)。以前每帧 `URL.createObjectURL`
|
||||
* 喂给 `<img>`,25fps 时单路就有几十个 object URL 同时存活(要等 1.2~1.5s 才 revoke),
|
||||
* 多路合成时内存/GC 抖动明显,且没法做叠加。这里改成「画一帧、复用同一块 canvas」:
|
||||
*
|
||||
* - 解码优先 `createImageBitmap`(后台线程),失败退回 `<img>` + object URL(用完即撤);
|
||||
* - 帧到得比画得快时**只留最新一帧**,画完再取,不会积压;
|
||||
* - 等比适应(含 HiDPI),`contain` = 留黑边(原 object-fit: contain),`cover` = 裁满(原 object-fit: cover)。
|
||||
*/
|
||||
|
||||
export type FrameFit = 'contain' | 'cover';
|
||||
|
||||
export interface FramePainter {
|
||||
/** 丢一帧进来(调用方不用 await,也不关心绘制时机) */
|
||||
push(blob: Blob): void;
|
||||
/** 清空画面与待画帧(会话还在,只是画面归零) */
|
||||
clear(): void;
|
||||
/** 彻底停用:之后的 push / 绘制一律作废 */
|
||||
dispose(): void;
|
||||
}
|
||||
|
||||
type DecodedFrame = ImageBitmap | HTMLImageElement;
|
||||
|
||||
/** 高分屏最多按 2 倍像素画,再高只是白烧 GPU/内存 */
|
||||
const MAX_DPR = 2;
|
||||
|
||||
function releaseFrame(frame: DecodedFrame) {
|
||||
if (typeof ImageBitmap !== 'undefined' && frame instanceof ImageBitmap) frame.close();
|
||||
}
|
||||
|
||||
async function decodeFrame(blob: Blob): Promise<DecodedFrame | null> {
|
||||
if (typeof createImageBitmap === 'function') {
|
||||
try {
|
||||
return await createImageBitmap(blob);
|
||||
} catch {
|
||||
// 某些浏览器/格式组合会解码失败,退回 <img>
|
||||
}
|
||||
}
|
||||
|
||||
return new Promise(resolve => {
|
||||
const url = URL.createObjectURL(blob);
|
||||
const img = new Image();
|
||||
img.onload = () => {
|
||||
URL.revokeObjectURL(url);
|
||||
resolve(img);
|
||||
};
|
||||
img.onerror = () => {
|
||||
URL.revokeObjectURL(url);
|
||||
resolve(null);
|
||||
};
|
||||
img.src = url;
|
||||
});
|
||||
}
|
||||
|
||||
export function createFramePainter(getCanvas: () => HTMLCanvasElement | null, fit: FrameFit = 'contain'): FramePainter {
|
||||
let latest: Blob | null = null;
|
||||
let painting = false;
|
||||
let disposed = false;
|
||||
|
||||
/**
|
||||
* 取 2d 上下文并同步 canvas 的像素尺寸
|
||||
*
|
||||
* - 尺寸用 `getBoundingClientRect()`(**渲染后**的 CSS 尺寸):工作台布局外层有 `transform: scale()`,
|
||||
* `clientWidth` 是缩放前的设计尺寸,按它算会白画好几倍像素;
|
||||
* - 缓冲区不超过帧本身的分辨率 —— 再大也只是把 JPEG 拉糊;
|
||||
* - canvas 还在隐藏/未挂载(尺寸为 0)时返回 null,这一帧丢掉即可,下一帧会重新尝试。
|
||||
*/
|
||||
function openContext(frameWidth: number, frameHeight: number): CanvasRenderingContext2D | null {
|
||||
const canvas = getCanvas();
|
||||
if (!canvas || !canvas.isConnected) return null;
|
||||
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
const cssWidth = rect.width || canvas.clientWidth;
|
||||
const cssHeight = rect.height || canvas.clientHeight;
|
||||
if (!cssWidth || !cssHeight) return null;
|
||||
|
||||
const dpr = Math.min(window.devicePixelRatio || 1, MAX_DPR);
|
||||
let width = Math.max(1, Math.round(cssWidth * dpr));
|
||||
let height = Math.max(1, Math.round(cssHeight * dpr));
|
||||
|
||||
// 等比缩到帧分辨率以内(cover/contain 都这样,保持画布宽高比不变)
|
||||
const shrink = Math.min(1, frameWidth / width, frameHeight / height);
|
||||
if (shrink < 1) {
|
||||
width = Math.max(1, Math.round(width * shrink));
|
||||
height = Math.max(1, Math.round(height * shrink));
|
||||
}
|
||||
|
||||
if (canvas.width !== width || canvas.height !== height) {
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
}
|
||||
|
||||
return canvas.getContext('2d');
|
||||
}
|
||||
|
||||
async function paint(blob: Blob) {
|
||||
const frame = await decodeFrame(blob);
|
||||
if (!frame) return;
|
||||
|
||||
try {
|
||||
if (disposed) return;
|
||||
|
||||
const ctx = openContext(frame.width, frame.height);
|
||||
if (!ctx) return;
|
||||
|
||||
const { canvas } = ctx;
|
||||
const scale = fit === 'cover'
|
||||
? Math.max(canvas.width / frame.width, canvas.height / frame.height)
|
||||
: Math.min(canvas.width / frame.width, canvas.height / frame.height);
|
||||
const drawWidth = Math.max(1, Math.round(frame.width * scale));
|
||||
const drawHeight = Math.max(1, Math.round(frame.height * scale));
|
||||
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
// cover 时目标矩形比画布大,负偏移即居中裁切
|
||||
ctx.drawImage(
|
||||
frame,
|
||||
Math.round((canvas.width - drawWidth) / 2),
|
||||
Math.round((canvas.height - drawHeight) / 2),
|
||||
drawWidth,
|
||||
drawHeight
|
||||
);
|
||||
} catch {
|
||||
// 单帧画失败(坏帧 / canvas 被释放)不影响后续帧
|
||||
} finally {
|
||||
releaseFrame(frame);
|
||||
}
|
||||
}
|
||||
|
||||
async function drain() {
|
||||
if (painting) return;
|
||||
painting = true;
|
||||
try {
|
||||
while (!disposed && latest) {
|
||||
const blob = latest;
|
||||
latest = null;
|
||||
await paint(blob);
|
||||
}
|
||||
} finally {
|
||||
painting = false;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
push(blob: Blob) {
|
||||
if (disposed) return;
|
||||
latest = blob;
|
||||
void drain();
|
||||
},
|
||||
clear() {
|
||||
latest = null;
|
||||
const canvas = getCanvas();
|
||||
const ctx = canvas?.getContext('2d');
|
||||
if (ctx) ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
||||
},
|
||||
dispose() {
|
||||
disposed = true;
|
||||
latest = null;
|
||||
},
|
||||
};
|
||||
}
|
||||
@ -19,6 +19,7 @@ import {
|
||||
} from '@/api/videoApi';
|
||||
import type { Camera, Nvr } from '@/api/video';
|
||||
import { localWs, type WsPtzCommand } from '@/api/localWs';
|
||||
import { createFramePainter, type FramePainter } from '@/utils/frameCanvas';
|
||||
import LocalServiceGate from '@/components/LocalServiceGate.vue';
|
||||
|
||||
const list = ref<Camera[]>([]);
|
||||
@ -203,11 +204,14 @@ const previewData = ref<PreviewData | null>(null);
|
||||
const previewFps = ref(25);
|
||||
/** 0-主码流(默认) 1-子码流 */
|
||||
const previewStreamType = ref(0);
|
||||
const previewSrc = ref('');
|
||||
/** 实时画面画布(帧直接画上来,不再每帧换 object URL) */
|
||||
const previewCanvas = ref<HTMLCanvasElement | null>(null);
|
||||
/** 是否已经收到过帧(决定显示「正在连接」还是「暂无画面」) */
|
||||
const previewHasFrame = ref(false);
|
||||
/** 当前会话的帧绘制器 */
|
||||
let previewPainter: FramePainter | null = null;
|
||||
/** WS 预览会话 sid(空 = 当前没有会话) */
|
||||
let wsSid = '';
|
||||
/** 当前 WS 帧的 object URL(每帧替换后要 revoke) */
|
||||
let wsFrameUrl = '';
|
||||
let wsUnsubscribe: (() => void) | null = null;
|
||||
/** 记住设备凭据(只在本页内存里,用来开原生窗口时不重复请求) */
|
||||
let previewCred: { host: string; port: number; username: string; password: string } | null = null;
|
||||
@ -224,7 +228,7 @@ const previewModeLabel = computed(() => {
|
||||
return '码流解码(最高 25fps)';
|
||||
});
|
||||
|
||||
/** 停掉 WS 预览:取消订阅 + 通知本机释放会话 + 回收帧 URL */
|
||||
/** 停掉 WS 预览:取消订阅 + 通知本机释放会话 + 停用帧绘制器 */
|
||||
function stopWsPreview() {
|
||||
wsUnsubscribe?.();
|
||||
wsUnsubscribe = null;
|
||||
@ -234,10 +238,11 @@ function stopWsPreview() {
|
||||
wsSid = '';
|
||||
}
|
||||
|
||||
if (wsFrameUrl) {
|
||||
URL.revokeObjectURL(wsFrameUrl);
|
||||
wsFrameUrl = '';
|
||||
}
|
||||
// 先作废再擦画布:避免解码中的那一帧在擦完后才落地
|
||||
previewPainter?.dispose();
|
||||
previewPainter?.clear();
|
||||
previewPainter = null;
|
||||
previewHasFrame.value = false;
|
||||
}
|
||||
|
||||
/** RTSP 展示用:遮掉密码,避免截图/录像时泄露 */
|
||||
@ -250,7 +255,7 @@ async function openPreview(camera: Camera, streamType = previewStreamType.value)
|
||||
previewStreamType.value = streamType;
|
||||
previewOpen.value = true;
|
||||
previewData.value = null;
|
||||
previewSrc.value = '';
|
||||
previewHasFrame.value = false;
|
||||
previewError.value = '';
|
||||
previewLoading.value = true;
|
||||
previewOffline.value = false;
|
||||
@ -310,12 +315,9 @@ async function openPreview(camera: Camera, streamType = previewStreamType.value)
|
||||
};
|
||||
|
||||
wsUnsubscribe = localWs.onFrame(info.sid, blob => {
|
||||
const url = URL.createObjectURL(blob);
|
||||
const old = wsFrameUrl;
|
||||
wsFrameUrl = url;
|
||||
previewSrc.value = url;
|
||||
// 给 <img> 一点时间切到新帧再释放旧 URL
|
||||
if (old) window.setTimeout(() => URL.revokeObjectURL(old), 1500);
|
||||
if (!previewPainter) previewPainter = createFramePainter(() => previewCanvas.value, 'contain');
|
||||
previewPainter.push(blob);
|
||||
previewHasFrame.value = true;
|
||||
});
|
||||
|
||||
if (info.warning) previewError.value = info.warning;
|
||||
@ -350,7 +352,6 @@ async function closePreview() {
|
||||
ptzError.value = '';
|
||||
previewOpen.value = false;
|
||||
stopWsPreview();
|
||||
previewSrc.value = '';
|
||||
previewData.value = null;
|
||||
previewCamera.value = null;
|
||||
previewError.value = '';
|
||||
@ -416,14 +417,6 @@ function switchStreamType(value: number) {
|
||||
if (camera) void openPreview(camera, previewStreamType.value);
|
||||
}
|
||||
|
||||
function onPreviewImgError() {
|
||||
// WS 每帧都换 object URL,偶发的 error 不用重连
|
||||
if (wsSid) return;
|
||||
|
||||
previewSrc.value = '';
|
||||
previewError.value = '画面中断:请确认设备可达、该通道已接入前端设备、账号密码有效。';
|
||||
}
|
||||
|
||||
async function copyRtsp() {
|
||||
if (!previewData.value) return;
|
||||
try {
|
||||
@ -664,9 +657,9 @@ async function doPreset(action: 'set' | 'goto' | 'clear') {
|
||||
<div v-if="previewError" class="alert alert-error">{{ previewError }}</div>
|
||||
|
||||
<div class="preview-stage">
|
||||
<canvas ref="previewCanvas" class="preview-canvas" aria-label="实时画面" />
|
||||
<div v-if="previewLoading" class="preview-tip">正在连接设备…</div>
|
||||
<img v-else-if="previewSrc" :src="previewSrc" alt="实时画面" @error="onPreviewImgError" />
|
||||
<div v-else class="preview-tip">暂无画面</div>
|
||||
<div v-else-if="!previewHasFrame" class="preview-tip">暂无画面</div>
|
||||
</div>
|
||||
|
||||
<div class="preview-toolbar">
|
||||
@ -807,16 +800,22 @@ async function doPreset(action: 'set' | 'goto' | 'clear') {
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.preview-stage img {
|
||||
.preview-canvas {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: contain;
|
||||
display: block;
|
||||
}
|
||||
|
||||
/* 提示浮在画布上:画布始终挂着(只画不重建),有无画面靠提示层说明 */
|
||||
.preview-tip {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
color: #94a3b8;
|
||||
font-size: 13px;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.preview-toolbar {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user