---
title: Camera, microphone and screen
summary: "The standard `navigator.mediaDevices` calls inside an extension frame, captured by the host and streamed in, with the `camera`, `microphone` and `screen` permissions."
audience: both
stability: experimental
since: protocol 1
---

# Camera, microphone and screen

Browsers refuse camera, microphone and screen capture to the extension frame's sandboxed, opaque origin. So the
SpaceMIX window around the frame captures for it and streams the tracks in over an in-page `RTCPeerConnection`.
The frame runtime puts its own versions of the standard calls on the frame's `navigator.mediaDevices`, so
extension code uses the ordinary web API:

```ts
navigator.mediaDevices.getUserMedia(constraints: MediaStreamConstraints): Promise<MediaStream>; // camera, microphone
navigator.mediaDevices.getDisplayMedia(options?: DisplayMediaStreamOptions): Promise<MediaStream>; // screen
navigator.mediaDevices.enumerateDevices(): Promise<MediaDeviceInfo[]>;
```

There is no `host.media` namespace.

**Permissions:** `camera` for `video`, `microphone` for `audio`, `screen` for `getDisplayMedia`. Without the one
a call needs, it rejects with a `DOMException` named `NotAllowedError`:
`The manifest doesn't ask for the "camera" permission (or it isn't approved yet).`

The browser still asks the person, once, for SpaceMIX's own origin (the desktop app allows it without asking).
Anyone on the team can turn capture off per extension in **Settings → Extensions → Media access**, for everyone on the server; that ends the
extension's running tracks (they fire `ended`) and refuses new ones. Only people with `server:settings` can turn it back on. Capture needs a
secure page: the desktop app, https or localhost.

## `getUserMedia`

- `audio` and `video` are each `false`, `true` or a constraints object; at least one must be set (otherwise a
  `TypeError`). Other members of the constraints are ignored.
- Resolves with a `MediaStream` of the host's tracks. Each track reports the device's `label`,
  `getSettings()`, `getCapabilities()` and `getConstraints()`, and forwards `applyConstraints()` to the host's
  track.
- `track.stop()` stops this copy; when every copy of a host track (`clone()` included) is stopped, the host
  releases the device.

## `getDisplayMedia`

- The person picks a screen, window or tab in the browser's picker, every time.
- **Call it directly in a click handler, with nothing awaited before it.** The click's user activation must reach
  the host; otherwise it rejects with `InvalidStateError`:
  `Screen sharing has to start from a click: call getDisplayMedia in the click handler, with nothing awaited before it.`
- `video` defaults to `true`; `audio` defaults to `false`. Of the other options only `preferCurrentTab`,
  `selfBrowserSurface`, `systemAudio`, `surfaceSwitching`, `monitorTypeSurfaces` and `windowAudio` are passed on.

## `enumerateDevices`

The devices of the kinds the extension may use: `videoinput` with `camera`, `audioinput` and `audiooutput` with
`microphone`. Labels are empty until the person has allowed capture once, as on any page. With only `screen`, or on
failure, it resolves with `[]`. `devicechange` events are not relayed.

## Errors

Every failure is a `DOMException` with a standard name, so code written for the web works:

| Name | When |
|---|---|
| `NotAllowedError` | permission missing, turned off in Settings, or the person said no |
| `NotSupportedError` | not a secure page, or the browser lacks `RTCPeerConnection` |
| `InvalidStateError` | screen sharing without a click; a track that has ended |
| `NotReadableError` | the device couldn't be passed into the frame (in time: 15 seconds) |
| `NotFoundError`, `OverconstrainedError`, … | as the browser reports them |

Failures are also reported to the dev console and the extension's status in the store, because extension code
often swallows them.

## Under the bridge (internal)

`install` in `packages/sdk/src/media.ts` (internal; called once by the runtime when the frame connects) puts the
three calls on the frame's `navigator.mediaDevices`. It installs them in every frame, so a call without the
permission gets the clear `NotAllowedError` above instead of the browser's refusal of the opaque origin. They use
these bridge methods; extensions don't call them directly. The wire shapes are `MediaWire` in
`packages/sdk/src/protocol.ts`.

| Method | Does |
|---|---|
| `media.open` (constraints, source `"user"` or `"display"`) | Checks permissions, captures, returns an SDP offer with one send-only transceiver per track |
| `media.answer` (session, sdp) | The frame's answer |
| `media.stop` (session, mid) | The frame is done with one track |
| `media.constraints` (session, mid, constraints) | `applyConstraints` on the host's track; returns its new settings |
| `media.devices` () | The device list above |

The `media` subscription channel (params `{ session }`) tells the frame when a host track ended. All of these need
`camera`, `microphone` or `screen`; which one is checked per call. Sessions belong to the frame that opened them and
are closed when it is dropped.

## Example

```ts
export async function startCamera(video: HTMLVideoElement): Promise<MediaStream> {
  const stream = await navigator.mediaDevices.getUserMedia({ video: true });
  video.srcObject = stream;
  await video.play();
  return stream;
}

export function stopAll(stream: MediaStream): void {
  for (const track of stream.getTracks()) track.stop();
}
```

`autoplay` is allowed in the frame for extensions with `camera`, `microphone` or `screen`, so remote audio in a call plays
without a click. For calls between people, `RTCPeerConnection` works in the frame; exchange offers and ICE
candidates through a collection, `host.storage` or `host.chat`, since there's no WebSocket.

## Stability

Experimental: the standard API surface is the contract, but the host capture has not been tested widely with real
devices yet.
