Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
fix(preview): eight ring slots, and a slow display's two frames as the hold floor
EtienneLescot099d54027ce0
diff --git a/crates/compositor/src/shared_frames.rs b/crates/compositor/src/shared_frames.rsindex a6b4f8415..cbc6eda8a 100644--- a/crates/compositor/src/shared_frames.rs+++ b/crates/compositor/src/shared_frames.rs@@ -16,9 +16,13 @@ use crate::frame_geometry::FootageQuad;use crate::live::FramePosition;use std::time::{Duration, Instant};-/// Cases de l'anneau : une prête, une en transit vers le renderer (tenue jusqu'à ce que-/// Chromium la relâche), une en écriture, et une de marge pour la latence de la libération.-pub const RING_SLOTS: u32 = 4;+/// Cases de l'anneau : une prête, une en écriture, et celles que Chromium tient encore. Une case+/// relâchée reste tenue 40 ms de plus côté Electron (`SHARED_SLOT_RELEASE_DELAY_MS`), le temps+/// que le GPU ait copié la frame dans le canvas : à 60 images/s, trois à quatre cases sont alors+/// tenues en même temps. Avec quatre, le rendu attendait une case libre et sautait des frames ;+/// huit laissent de la marge jusqu'à ~150 images/s, pour 8 × (largeur × hauteur × 4) octets de+/// VRAM (66 Mo en 1080p).+pub const RING_SLOTS: u32 = 8;/// Une case tenue plus longtemps que ça a perdu sa libération (renderer rechargé, import/// échoué en route) : Chromium relâche les autres dans la milliseconde. Même ordre que lediff --git a/electron/native-bridge/services/compositorViewService.test.ts b/electron/native-bridge/services/compositorViewService.test.tsindex a7791c58f..36e4048c3 100644--- a/electron/native-bridge/services/compositorViewService.test.ts+++ b/electron/native-bridge/services/compositorViewService.test.ts@@ -11,6 +11,7 @@ import {ffmpegSharedBinCandidates,resolveSceneAssetPaths,SHARED_SLOT_RELEASE_DELAY_MS,+ sharedSlotHoldMs,} from "./compositorViewService";describe("native GIF cancellation capability", () => {@@ -113,7 +114,13 @@ describe("CompositorViewService frames handed over as shared GPU textures", () =footageProjective: false,};- function setup(options: { addon?: Partial<CompositorViewAddon>; gpuCompositing?: boolean } = {}) {+ function setup(+ options: {+ addon?: Partial<CompositorViewAddon>;+ gpuCompositing?: boolean;+ displayHz?: number | null;+ } = {},+ ) {const addon = {createView: vi.fn(() => 7),setSharedFrames: vi.fn(() => true),@@ -136,6 +143,7 @@ describe("CompositorViewService frames handed over as shared GPU textures", () =addon: addon as unknown as CompositorViewAddon,sharedTexture,gpuCompositing: () => options.gpuCompositing ?? true,+ displayHz: () => options.displayHz ?? 60,});const id = service.createView({ x: 0, y: 0, width: 4, height: 2 });return {@@ -204,6 +212,58 @@ describe("CompositorViewService frames handed over as shared GPU textures", () =}});+ it("holds the slot two frames of a slow display, never less than the floor", async () => {+ expect(sharedSlotHoldMs(60)).toBe(SHARED_SLOT_RELEASE_DELAY_MS);+ expect(sharedSlotHoldMs(144)).toBe(SHARED_SLOT_RELEASE_DELAY_MS);+ expect(sharedSlotHoldMs(30)).toBe(67);+ expect(sharedSlotHoldMs(null)).toBe(SHARED_SLOT_RELEASE_DELAY_MS);+ expect(sharedSlotHoldMs(0)).toBe(SHARED_SLOT_RELEASE_DELAY_MS);++ vi.useFakeTimers();+ try {+ const { addon, id, releaseEverywhere, service } = setup({ displayHz: 30 });+ await service.readFrame(id, 0, target);+ releaseEverywhere();+ vi.advanceTimersByTime(66);+ expect(addon.releaseSharedFrame).not.toHaveBeenCalled();+ vi.advanceTimersByTime(1);+ expect(addon.releaseSharedFrame).toHaveBeenCalledWith(id, 1, 3);+ } finally {+ vi.useRealTimers();+ }+ });++ it("holds every frame in flight on its own clock, so none of them loses its slot", async () => {+ vi.useFakeTimers();+ try {+ const frames = [+ { ...sharedFrame, gen: 3, slot: 1 },+ { ...sharedFrame, gen: 4, slot: 2 },+ ];+ const { addon, id, service, sharedTexture } = setup({+ addon: { readSharedFrame: vi.fn(() => frames.shift() ?? null) },+ });+ const released: Array<() => void> = [];+ sharedTexture.importSharedTexture.mockImplementation((options) => {+ released.push(options.allReferencesReleased as () => void);+ return { release: vi.fn() } as unknown as Electron.SharedTextureImported;+ });+ await service.readFrame(id, 0, target);+ await service.readFrame(id, 3, target);++ released[0]();+ vi.advanceTimersByTime(20);+ released[1]();+ vi.advanceTimersByTime(SHARED_SLOT_RELEASE_DELAY_MS - 20);+ expect(addon.releaseSharedFrame).toHaveBeenCalledTimes(1);+ expect(addon.releaseSharedFrame).toHaveBeenLastCalledWith(id, 1, 3);+ vi.advanceTimersByTime(20);+ expect(addon.releaseSharedFrame).toHaveBeenLastCalledWith(id, 2, 4);+ } finally {+ vi.useRealTimers();+ }+ });+it("goes back to read-back when a delivery fails", async () => {const { addon, id, service, sharedTexture } = setup();sharedTexture.sendSharedTexture.mockRejectedValueOnce(new Error("timed out after 1000ms"));diff --git a/electron/native-bridge/services/compositorViewService.ts b/electron/native-bridge/services/compositorViewService.tsindex 078f53877..8bb4e888a 100644--- a/electron/native-bridge/services/compositorViewService.ts+++ b/electron/native-bridge/services/compositorViewService.ts@@ -2,7 +2,14 @@ import fs from "node:fs";import { createRequire } from "node:module";import path from "node:path";import { fileURLToPath } from "node:url";-import { app, sharedTexture, type WebFrameMain } from "electron";+import {+ app,+ BrowserWindow,+ screen,+ sharedTexture,+ type WebFrameMain,+ webContents,+} from "electron";import type { ShaderEffectCheck } from "../../../src/lib/ai-edition/shaderEffects";import {type CursorKind,@@ -231,6 +238,9 @@ export interface CompositorViewServiceOptions {/** Whether Chromium composites on the GPU, where a shared texture is imported. Injectable* for tests; defaults to `app.getGPUFeatureStatus()`. */gpuCompositing?: () => boolean;+ /** Refresh rate of the display the frame's window is on, `null` when unknown. Injectable for+ * tests; defaults to Electron's `screen`. */+ displayHz?: (target: WebFrameMain) => number | null;}/**@@ -239,11 +249,28 @@ export interface CompositorViewServiceOptions {* has issued the draw into the canvas, not once the GPU has run it: the copy runs when the frame* carrying it is drawn, up to a vsync later. A slot written again before that copy put a later* frame on the canvas, then the one in between, so the picture and the cursor stepped back a frame- * (tens of times in 18 s at 60 Hz, a few at 280 Hz). 40 ms is more than two vsyncs at 60 Hz; it- * took the steps back to none at both rates, and the four slots still keep up with 60 fps.+ * (tens of times in 18 s at 60 Hz, a few at 280 Hz). 40 ms took the steps back to none at both+ * rates. On a slower display the copy can come later still: see `sharedSlotHoldMs`.*/export const SHARED_SLOT_RELEASE_DELAY_MS = 40;+/** How long to hold a released slot for a window on a `hz` display: two of its frames, never less+ * than `SHARED_SLOT_RELEASE_DELAY_MS` (67 ms at 30 Hz, 40 ms from 50 Hz up). */+export function sharedSlotHoldMs(hz: number | null): number {+ if (!hz || !Number.isFinite(hz) || hz <= 0) return SHARED_SLOT_RELEASE_DELAY_MS;+ return Math.max(SHARED_SLOT_RELEASE_DELAY_MS, Math.ceil(2000 / hz));+}++function defaultDisplayHz(target: WebFrameMain): number | null {+ try {+ const contents = webContents.fromFrame(target);+ const win = contents && BrowserWindow.fromWebContents(contents);+ return win ? screen.getDisplayMatching(win.getBounds()).displayFrequency : null;+ } catch {+ return null;+ }+}+/** The two calls a shared preview frame needs from Electron's `sharedTexture` module. */export type SharedTextureApi = Pick<Electron.SharedTexture,@@ -746,6 +773,7 @@ export class CompositorViewService {sourceTimeSec: frame.sourceTimeSec,};const api = this.sharedTextureApi();+ const holdMs = sharedSlotHoldMs((this.options.displayHz ?? defaultDisplayHz)(target));let imported: Electron.SharedTextureImported | undefined;try {if (!api) {@@ -760,10 +788,7 @@ export class CompositorViewService {// Chromium is done with the texture in every process: after a grace period, the slot// may be written again (SHARED_SLOT_RELEASE_DELAY_MS says why it waits).allReferencesReleased: () => {- setTimeout(- () => addon.releaseSharedFrame?.(id, frame.slot, frame.gen),- SHARED_SLOT_RELEASE_DELAY_MS,- );+ setTimeout(() => addon.releaseSharedFrame?.(id, frame.slot, frame.gen), holdMs);},});await api.sendSharedTexture({ frame: target, importedSharedTexture: imported }, meta);