diff --git a/apps/mobile/src/features/browser/preview-stream.browser.ts b/apps/mobile/src/features/browser/preview-stream.browser.ts index 748cecd982f93..e0c88b26e8a8b 100644 --- a/apps/mobile/src/features/browser/preview-stream.browser.ts +++ b/apps/mobile/src/features/browser/preview-stream.browser.ts @@ -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(() => { diff --git a/apps/server/src/preview/ServerBrowser.test.ts b/apps/server/src/preview/ServerBrowser.test.ts index a15378ec3039c..a90b4335857d7 100644 --- a/apps/server/src/preview/ServerBrowser.test.ts +++ b/apps/server/src/preview/ServerBrowser.test.ts @@ -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({ + 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", () => diff --git a/apps/server/src/preview/ServerBrowser.ts b/apps/server/src/preview/ServerBrowser.ts index 26203fc1a63c8..d1f315d0e263f 100644 --- a/apps/server/src/preview/ServerBrowser.ts +++ b/apps/server/src/preview/ServerBrowser.ts @@ -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; @@ -3245,6 +3247,7 @@ const make = Effect.gen(function* () { const recentFrames: Array = []; 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 () => { @@ -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); }); @@ -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); diff --git a/apps/server/src/preview/ServerBrowserStream.ts b/apps/server/src/preview/ServerBrowserStream.ts index 649af3b31d0db..242338de9651f 100644 --- a/apps/server/src/preview/ServerBrowserStream.ts +++ b/apps/server/src/preview/ServerBrowserStream.ts @@ -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( diff --git a/packages/client-runtime/src/preview/serverBrowserStream.ts b/packages/client-runtime/src/preview/serverBrowserStream.ts index 24d1cddd62fc8..7ce7801f52f54 100644 --- a/packages/client-runtime/src/preview/serverBrowserStream.ts +++ b/packages/client-runtime/src/preview/serverBrowserStream.ts @@ -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 } @@ -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 | null = null; @@ -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; @@ -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));