v159 · dom · media session
MediaSession: Return Promise<undefined> from setMicrophoneActive() and setCameraActive()
Video-call sites tell the browser “my mic is muted” or “my camera is off” with navigator.mediaSession.setMicrophoneActive() and setCameraActive(), so the browser’s own capture UI can offer matching togglemicrophone / togglecamera buttons. Since Chrome 93 those methods returned undefined. The W3C spec (PR #312) made them return Promise<undefined> so a browser can prompt or apply a capture-pause policy asynchronously — and Chrome 159 catches up with Safari. The return shape changes, and so does how errors arrive.
concepts
-
Return-shape probe
The canonical observation. Call each method with
trueorfalseand look at exactly what comes back —typeof, itsSymbol.toStringTag,instanceof Promise— then await it and see it settle withundefined. No camera or microphone permission needed. -
IDL contract inspector
Every observable consequence of the new WebIDL signature in one table: return type,
.length, boolean coercion, too-few-arguments and wrong-receiver errors (synchronous throw vs rejected promise), and the spec-onlysetScreenshareActive()sibling Chromium has not shipped. -
Video-call mute controls
The real product case: a call control bar whose mute and camera buttons await the browser’s acknowledgement before updating, and which registers
togglemicrophone,togglecameraandhangupaction handlers so the browser’s capture UI can drive it. One normalising line keeps it working on pre-159 browsers. -
Detached-document rejection
The spec’s first step: a session whose document is not fully active must return a promise rejected with
InvalidStateError. Build an iframe, keep itsmediaSession, remove the frame, and call it — pre-159 Chrome silently returnedundefined. -
Caller-pattern migration matrix
Four ways existing code calls these methods — fire-and-forget,
try/catch,awaitinsidetry, and a normalising shim — run for real against three scenarios. See which errors each pattern catches, which become unhandled rejections, and which code needs migrating.
why it shipped
The spec’s update capture state algorithm runs “in parallel”: a user agent that pauses all microphones from its own UI may wait, prompt the user, and reject with NotAllowedError if the page asks to re-activate capture without permission. A synchronous void method cannot express that, so the spec changed the return type. Chromium does not prompt or apply a pause policy — its implementation updates the capture state through the media session service and returns an already-resolved promise — but returning a promise aligns the IDL with Safari and the spec and fixes four WPT idlharness subtests.
Compatibility risk is low: code that ignores the return value keeps working, because in a fully active document the promise always resolves. What does change is error delivery. WebIDL converts exceptions in promise-returning operations into rejected promises, so a missing argument or wrong receiver that used to throw now rejects, and calling into a detached document — previously a silent no-op — now rejects with InvalidStateError.
Flag: none. There is no runtime_enabled_features.json5 entry for this change; it ships enabled for everyone in Chrome 159. To try it before stable, use Chrome Canary, Dev or Beta at version 159 or later.
the API
// WebIDL (W3C Media Session, Chromium 159+)
Promise<undefined> setMicrophoneActive(boolean active);
Promise<undefined> setCameraActive(boolean active);
const r = navigator.mediaSession.setMicrophoneActive(false);
r instanceof Promise // Chrome ≤158: false (r === undefined)
// Chrome 159+: true
await r; // resolves with undefined in a fully active document
// Works on both: normalise before chaining
await Promise.resolve(navigator.mediaSession.setCameraActive(true));
references
- ChromeStatus entry
- W3C Media Session — setMicrophoneActive() and the update capture state algorithm
- w3c/mediasession PR #312 — return Promise<undefined>
- MDN: MediaSession.setMicrophoneActive()
- MDN: MediaSession.setCameraActive()
- crbug.com/576328036 — implementation tracking
- Chromium source: media_session.idl
- WPT: mediasession/idlharness.window.html
- WebKit bug 277421 · Mozilla standards position #504