Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions apps/desktop/src/preview/Manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ describe("fitPictureInPictureContentSize", () => {
});
});

describe("pictureInPictureJPEGQuality", () => {
it("streams sharper frames only while the PiP window is enlarged", () => {
expect(PreviewManager.pictureInPictureJPEGQuality(false)).toBe(80);
expect(PreviewManager.pictureInPictureJPEGQuality(true)).toBe(90);
});
});

describe("recordingFileExtension", () => {
it("derives the artifact extension from the recorder's actual mime type", () => {
expect(PreviewManager.recordingFileExtension("video/mp4;codecs=avc1.640028")).toBe("mp4");
Expand Down Expand Up @@ -2675,6 +2682,8 @@ describe("PreviewManager", () => {
alwaysOnTop: true,
show: false,
skipTaskbar: true,
fullscreenable: true,
maximizable: true,
webPreferences: expect.objectContaining({
preload: "/tmp/t3/desktop/preview-pip-preload.cjs",
backgroundThrottling: false,
Expand Down Expand Up @@ -2738,6 +2747,59 @@ describe("PreviewManager", () => {
),
);

effectIt.effect("keeps an enlarged PiP window enlarged and reapplies aspect on restore", () =>
withManager((manager) =>
Effect.gen(function* () {
const jpegWide = Buffer.from("pip-frame-1280x720");
const jpegTall = Buffer.from("pip-frame-640x480");
let size = { width: 1280, height: 720 };
let bytes = jpegWide;
const toJPEG = vi.fn((_quality?: number) => bytes);
const capturePage = vi.fn(async () => ({
toJPEG,
getSize: () => ({ ...size }),
}));
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
let maximized = false;
const { pictureInPictureWindow, send } = makeTestPictureInPictureWindow();
Object.assign(pictureInPictureWindow, {
isMaximized: vi.fn(() => maximized),
isFullScreen: vi.fn(() => false),
});
browserWindowConstructor.mockImplementation(function () {
return pictureInPictureWindow;
});

yield* manager.createTab("tab_pip_enlarged");
yield* manager.registerWebview("tab_pip_enlarged", 42);
yield* manager.openPictureInPicture("tab_pip_enlarged");

expect(toJPEG.mock.calls.at(-1)).toEqual([80]);
expect(pictureInPictureWindow.setAspectRatio.mock.calls).toEqual([[0], [1280 / 720]]);
const contentSizesAfterOpen = pictureInPictureWindow.setContentSize.mock.calls.length;

maximized = true;
size = { width: 640, height: 480 };
bytes = jpegTall;
yield* TestClock.adjust(100);

expect(toJPEG.mock.calls.at(-1)).toEqual([90]);
expect(send).toHaveBeenCalledTimes(2);
expect(pictureInPictureWindow.setContentSize.mock.calls.length).toBe(contentSizesAfterOpen);

maximized = false;
yield* TestClock.adjust(100);

expect(toJPEG.mock.calls.at(-1)).toEqual([80]);
expect(pictureInPictureWindow.setContentSize.mock.calls.length).toBe(
contentSizesAfterOpen + 1,
);
expect(pictureInPictureWindow.setAspectRatio.mock.calls.at(-1)).toEqual([640 / 480]);
yield* manager.closePictureInPicture("tab_pip_enlarged");
}),
),
);

effectIt.effect("keeps picture-in-picture capture separate from recording warmup", () =>
withManager((manager) =>
Effect.gen(function* () {
Expand Down
83 changes: 50 additions & 33 deletions apps/desktop/src/preview/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ const MAX_SCREENSHOT_WIDTH = 1280;
const RECORDING_ARM_GRACE_MS = 10_000;
const PICTURE_IN_PICTURE_FRAME_INTERVAL_MS = Math.ceil(1_000 / 12);
const PICTURE_IN_PICTURE_JPEG_QUALITY = 80;
const PICTURE_IN_PICTURE_JPEG_QUALITY_ENLARGED = 90;
const PICTURE_IN_PICTURE_INITIAL_WIDTH = 480;
const PICTURE_IN_PICTURE_INITIAL_HEIGHT = 320;
const PICTURE_IN_PICTURE_MIN_WIDTH = 240;
Expand Down Expand Up @@ -193,6 +194,11 @@ export const fitPictureInPictureContentSize = (
return [Math.round(width), Math.round(height)];
};

// Sharper frames only while the PiP window is enlarged; the small window keeps
// the cheaper default so idle streaming cost does not change.
export const pictureInPictureJPEGQuality = (isEnlarged: boolean): number =>
isEnlarged ? PICTURE_IN_PICTURE_JPEG_QUALITY_ENLARGED : PICTURE_IN_PICTURE_JPEG_QUALITY;

export const recordingFileExtension = (mimeType: string): string => {
const subtype = mimeType.split(";", 1)[0]?.trim().toLowerCase().split("/")[1] ?? "";
const extension = subtype.replace(/^x-/, "").replace(/[^a-z0-9]/g, "");
Expand Down Expand Up @@ -2709,16 +2715,57 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
) {
return;
}
const pipWindow = (yield* SynchronizedRef.get(pictureInPictureSessionsRef)).get(tabId)?.window;
// A maximized or fullscreen PiP window is OS-sized, so aspect-fit resizes must
// not fight it; frames stay sharp while enlarged via the higher JPEG quality.
const pictureInPictureEnlarged =
pipWindow !== undefined &&
!pipWindow.isDestroyed() &&
(pipWindow.isMaximized?.() === true || pipWindow.isFullScreen?.() === true);
const encoded = yield* attempt(
{
operation: "frameCapture.encodeFrame",
tabId,
webContentsId: wc.id,
},
() => image.toJPEG(PICTURE_IN_PICTURE_JPEG_QUALITY),
() => image.toJPEG(pictureInPictureJPEGQuality(pictureInPictureEnlarged)),
);
const frameSession = (yield* SynchronizedRef.get(frameCaptureSessionsRef)).get(tabId);
if (frameSession?.scope !== captureSession.scope) return;
// Track the source aspect on every capture so a ratio change while enlarged is
// applied on restore even when later frames are byte-identical. The stored
// ratio is only updated alongside an apply, keeping it equal to the OS lock.
if (pipWindow !== undefined && !pipWindow.isDestroyed()) {
const previousAspectRatio = (yield* Ref.get(pictureInPictureAspectRatiosRef)).get(tabId);
const aspectRatio = size.width / size.height;
if (
!pictureInPictureEnlarged &&
(previousAspectRatio === undefined ||
Math.abs(previousAspectRatio - aspectRatio) > PICTURE_IN_PICTURE_ASPECT_RATIO_EPSILON)
) {
yield* attempt(
{
operation: "pictureInPicture.setAspectRatio",
tabId,
webContentsId: wc.id,
},
() => {
const contentSize = fitPictureInPictureContentSize(
pipWindow.getContentSize(),
aspectRatio,
);
pipWindow.setAspectRatio(0);
pipWindow.setContentSize(contentSize[0], contentSize[1], false);
pipWindow.setAspectRatio(aspectRatio);
},
);
yield* Ref.update(pictureInPictureAspectRatiosRef, (aspectRatios) =>
replaceMap(aspectRatios, (copy) => {
copy.set(tabId, aspectRatio);
}),
);
}
}
const pictureInPicture =
frameSession.consumers.has("picture-in-picture") &&
frameSession.lastPictureInPictureFrame?.equals(encoded) !== true;
Expand All @@ -2739,36 +2786,6 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
if (pictureInPictureWindow && !pictureInPictureWindow.isDestroyed()) {
deliveries.push(
Effect.gen(function* () {
const previousAspectRatio = (yield* Ref.get(pictureInPictureAspectRatiosRef)).get(
tabId,
);
const aspectRatio = frame.width / frame.height;
if (
previousAspectRatio === undefined ||
Math.abs(previousAspectRatio - aspectRatio) > PICTURE_IN_PICTURE_ASPECT_RATIO_EPSILON
) {
yield* attempt(
{
operation: "pictureInPicture.setAspectRatio",
tabId,
webContentsId: wc.id,
},
() => {
const contentSize = fitPictureInPictureContentSize(
pictureInPictureWindow.getContentSize(),
aspectRatio,
);
pictureInPictureWindow.setAspectRatio(0);
pictureInPictureWindow.setContentSize(contentSize[0], contentSize[1], false);
pictureInPictureWindow.setAspectRatio(aspectRatio);
},
);
yield* Ref.update(pictureInPictureAspectRatiosRef, (aspectRatios) =>
replaceMap(aspectRatios, (copy) => {
copy.set(tabId, aspectRatio);
}),
);
}
yield* attempt(
{
operation: "pictureInPicture.deliverFrame",
Expand Down Expand Up @@ -3004,8 +3021,8 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
show: false,
alwaysOnTop: true,
autoHideMenuBar: true,
fullscreenable: false,
maximizable: false,
fullscreenable: true,
maximizable: true,
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
minimizable: false,
resizable: true,
skipTaskbar: true,
Expand Down
Loading