Skip to content
Open
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
6 changes: 6 additions & 0 deletions apps/mobile/src/features/browser/preview-stream.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ export function start(configuration: PreviewStreamConfiguration) {
const height = Math.round((interactive ? next.height : side) * ratio);
if (cap && cap.width >= width && cap.height >= height) return;
cap = { width: Math.max(width, cap?.width ?? 0), height: Math.max(height, cap?.height ?? 0) };
// A new socket would release control, and agent-owned tabs do not hand it back.
if (
control?.controller === "you" &&
send({ type: "maxSize", maxWidth: cap.width, maxHeight: cap.height })
)
return;
connect();
};
const observer = new ResizeObserver(() => {
Expand Down
24 changes: 24 additions & 0 deletions apps/server/src/preview/ServerBrowser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,30 @@ it.effect("keeps a tab's zoom when its viewer resizes", () =>
).pipe(Effect.provide(layer)),
);

it.effect("raises a viewer's frame cap on its open stream without releasing control", () =>
Effect.scoped(
Effect.gen(function* () {
const { browser, broker, tabId } = yield* ready;
const viewer = yield* browser.attachViewer(viewerInput(tabId, true));
yield* viewer.input({ type: "takeControl" });
const session = contexts[0]!.sessions.at(-1)!;
session.send.mockClear();
yield* viewer.input({ type: "maxSize", maxWidth: 2064, maxHeight: 9000 });
expect(session.send).toHaveBeenCalledWith(
"Page.startScreencast",
expect.objectContaining({ maxWidth: 2064, maxHeight: 4320 }),
);
const status = yield* broker.invoke<PreviewAutomationStatus>({
scope,
tabId,
operation: "status",
input: {},
});
expect(status.control).toMatchObject({ owner: "human" });
}),
).pipe(Effect.provide(layer)),
);

it.effect(
"streams to a read-only viewer without allowing takeover, input, or viewport changes",
() =>
Expand Down
21 changes: 18 additions & 3 deletions apps/server/src/preview/ServerBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ const HOST_RECONNECT_DELAY = "1 second";
const DESKTOP_ATTACH_TIMEOUT = "10 seconds";
// A viewer still is best-effort; a capture Chromium never answers must not hold the capture lock.
const VIEWER_STILL_TIMEOUT_MS = 2_000;
export const MAX_STREAM_WIDTH = 7680;
export const MAX_STREAM_HEIGHT = 4320;
const VIEWER_OUTPUT_LIMIT = 64;
const RECORDING_SCREENCAST = { format: "jpeg", quality: 90, everyNthFrame: 1 } as const;
const RECORDING_BINARY_EXPORT_MIN_BYTES = 8 * 1024 * 1024;
Expand Down Expand Up @@ -3245,6 +3247,7 @@ const make = Effect.gen(function* () {
const recentFrames: Array<number> = [];
let screencastParams = Promise.resolve();
let screencastScale = 1;
let maxSize = { width: input.maxWidth, height: input.maxHeight };
const startScreencast = (scale: number) => {
screencastScale = scale;
screencastParams = screencastParams.then(async () => {
Expand All @@ -3254,8 +3257,8 @@ const make = Effect.gen(function* () {
.send("Page.startScreencast", {
format: "jpeg",
quality: screencastScale < 1 ? Math.min(quality, SCREENCAST_MOTION_QUALITY) : quality,
maxWidth: Math.max(1, Math.round(input.maxWidth * screencastScale)),
maxHeight: Math.max(1, Math.round(input.maxHeight * screencastScale)),
maxWidth: Math.max(1, Math.round(maxSize.width * screencastScale)),
maxHeight: Math.max(1, Math.round(maxSize.height * screencastScale)),
})
.catch(constVoid);
});
Expand Down Expand Up @@ -3395,9 +3398,21 @@ const make = Effect.gen(function* () {
output,
input: (raw: unknown) =>
Effect.promise(async () => {
if (!viewer.canOperate) return;
const message = asRecord(raw);
if (!message) return;
// Raising the cap in place keeps this viewer's control, which a new socket would lose.
if (message.type === "maxSize") {
const width = num(message.maxWidth);
const height = num(message.maxHeight);
if (!(width > 0 && height > 0)) return;
maxSize = {
width: Math.min(Math.round(width), MAX_STREAM_WIDTH),
height: Math.min(Math.round(height), MAX_STREAM_HEIGHT),
};
await startScreencast(screencastScale);
return;
}
if (!viewer.canOperate) return;
try {
if (message.type === "takeControl") {
const taking = tab.control.take(viewer.id);
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/preview/ServerBrowserStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ const makeHandler = Effect.fnUntraced(function* (browser: ServerBrowser.ServerBr
threadId,
tabId,
canOperate,
maxWidth: intParam(params, "maxWidth", 1280, 7680),
maxHeight: intParam(params, "maxHeight", 800, 4320),
maxWidth: intParam(params, "maxWidth", 1280, ServerBrowser.MAX_STREAM_WIDTH),
maxHeight: intParam(params, "maxHeight", 800, ServerBrowser.MAX_STREAM_HEIGHT),
quality: intParam(params, "quality", DEFAULT_QUALITY, 100),
})
.pipe(
Expand Down
14 changes: 12 additions & 2 deletions packages/client-runtime/src/preview/serverBrowserStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export type PreviewStreamInput =
}
| { readonly type: "text"; readonly text: string }
| { readonly type: "resize"; readonly width: number; readonly height: number }
| { readonly type: "maxSize"; readonly maxWidth: number; readonly maxHeight: number }
| { readonly type: "navigate"; readonly url: string }
| { readonly type: "history"; readonly delta: -1 | 1 }
| { readonly type: "reload"; readonly ignoreCache?: boolean }
Expand Down Expand Up @@ -224,7 +225,8 @@ export function createPreviewStreamClient(
maxHeight: String(Math.max(1, Math.round(target.maxHeight))),
});
if (target.interactive === false) query.set("interactive", "false");
const url = withDeviceHubQuery(`${target.access.wsBase}/ws?${query.toString()}`, target.access);
const url = () =>
withDeviceHubQuery(`${target.access.wsBase}/ws?${query.toString()}`, target.access);
let stopped = false;
let socket: WebSocket | null = null;
let retryTimer: ReturnType<typeof setTimeout> | null = null;
Expand All @@ -241,7 +243,7 @@ export function createPreviewStreamClient(

const connect = () => {
if (stopped) return;
const ws = new WebSocket(url);
const ws = new WebSocket(url());
ws.binaryType = "arraybuffer";
socket = ws;
let opened = false;
Expand Down Expand Up @@ -389,7 +391,15 @@ export function createPreviewStreamClient(

return {
send: (input) => {
if (input.type === "maxSize") {
query.set("maxWidth", String(Math.max(1, Math.round(input.maxWidth))));
query.set("maxHeight", String(Math.max(1, Math.round(input.maxHeight))));
}
if (socket?.readyState !== WebSocket.OPEN) return false;
if (input.type === "maxSize") {
socket.send(JSON.stringify(input));
return true;
}
if (!control?.canOperate) return false;
if (input.type !== "takeControl" && control.controller !== "you") return false;
socket.send(JSON.stringify(input));
Expand Down
Loading