Freestyle Docs

Freestyle / Guides

Stream and control a VM display from the browser

Show a sandbox desktop in your web app and send mouse and keyboard input with a Freestyle identity.

Use the Display API to let someone interact with a running GPU sandbox or another sandbox with display access enabled. The Display API and GPUs are in beta and available by request. Request beta access for your team. You need a guest window or desktop to share, freestyle@0.2.24 or later, and an HTTPS viewer with WebCodecs support. For a live view of the public GPU Chromium snapshot, follow Watch and control Chromium in a sandbox.

Give the user access

On your server, verify that the signed-in user may control the VM. Desktop access requires a whole-VM grant, which also allows shell access. Create an identity once and save its ID with the user.

import { Freestyle } from "freestyle";

const admin = new Freestyle({ apiKey: process.env.FREESTYLE_API_KEY });
const { identity } = await admin.identities.create();
await identity.permissions.vm.grant({ vmId });

When that user opens the viewer, issue a short-lived token from your authenticated session endpoint. Keep your account API key on the server.

const { token } = await admin.identities.ref(identityId).tokens.create({
  expiresAt: new Date(Date.now() + 15 * 60_000).toISOString(),
});
return Response.json({ vmId, identityAccessToken: token }, {
  headers: { "Cache-Control": "no-store" },
});

Show the display

Add a <canvas> to the viewer. This example opens the first available display. If your app offers a window picker, pass the selected display ID to stream instead.

import { Freestyle } from "freestyle";

const { vmId, identityAccessToken } = await fetch("/api/display-session").then(r => r.json());
const displays = new Freestyle({ identityAccessToken }).vms.ref(vmId).displays;
const { displays: [display] } = await displays.list();
if (!display) throw new Error("Start a graphical desktop or open a window in the sandbox first.");

const canvas = document.querySelector("canvas")!;
const context = canvas.getContext("2d")!;
const decoder = new VideoDecoder({
  output(frame) {
    try { context.drawImage(frame, 0, 0, canvas.width, canvas.height); }
    finally { frame.close(); }
  },
  error: console.error,
});
const stream = displays.stream(display.id, {
  codec: "h264",
  fps: 30,
  onConfig(config) {
    canvas.width = config.codedWidth!;
    canvas.height = config.codedHeight!;
    decoder.configure(config);
  },
  onChunk: chunk => decoder.decode(new EncodedVideoChunk(chunk)),
  onError: console.error,
  onClose() {
    canvas.onpointermove = canvas.onclick = null;
    if (decoder.state !== "closed") decoder.close();
  },
});

The browser decodes each frame and draws it directly onto the canvas. Call stream.close() when the viewer unmounts or the user signs out.

Send mouse and keyboard input

Use the open stream for input. Map the pointer’s position on the displayed image to the VM’s coordinate range of 0–32767 on each axis.

function movePointer(event: MouseEvent) {
  const rect = canvas.getBoundingClientRect();
  if (!rect.width || !rect.height) return;
  const scale = (n: number) => Math.round(32767 * Math.max(0, Math.min(1, n)));
  stream.sendInput({ pointer: [[{
    type: "move_absolute",
    x: scale((event.clientX - rect.left) / rect.width),
    y: scale((event.clientY - rect.top) / rect.height),
  }]] });
}

canvas.onpointermove = event => {
  if (decoder.state === "configured") movePointer(event);
};
canvas.onclick = event => {
  if (decoder.state !== "configured") return;
  movePointer(event);
  stream.sendInput({ pointer: [
    [{ type: "press", button: "left" }],
    [{ type: "release", button: "left" }],
  ] });
};

Keyboard input uses Linux evdev key codes. To send the A key, send its press and release together.

stream.sendInput({ keyboard: [
  [{ type: "press", key: 30 }],
  [{ type: "release", key: 30 }],
] });
esc