WebRTC Cheatsheet
Screen Sharing
Use this WebRTC reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
getDisplayMedia — Basic Usage
// Prompt user to pick a screen, window, or tab const screenStream = await navigator.mediaDevices.getDisplayMedia({ video: true, audio: false, // system audio (limited browser support) }); const [screenTrack] = screenStream.getVideoTracks(); document.getElementById('preview').srcObject = screenStream;
getDisplayMediamust be called from a user gesture (click, keypress). It always shows the browser's native picker — you cannot bypass it.
Options and Constraints
const stream = await navigator.mediaDevices.getDisplayMedia({ video: { displaySurface: 'monitor', // 'browser' | 'window' | 'monitor' width: { ideal: 1920, max: 3840 }, height: { ideal: 1080, max: 2160 }, frameRate: { ideal: 30, max: 60 }, cursor: 'always', // 'always' | 'motion' | 'never' (deprecated) }, audio: { echoCancellation: false, // recommended for system audio noiseSuppression: false, sampleRate: 44100, }, // Preferences (Chrome 107+) preferCurrentTab: false, // hint to pre-select current tab selfBrowserSurface: 'exclude', // 'include' | 'exclude' systemAudio: 'include', // 'include' | 'exclude' surfaceSwitching: 'include', // show/hide the "Switch" button mid-share monitorTypeSurfaces: 'include',// show/hide monitor option });
displaySurface Values
| Value | What the user sees |
|---|---|
"browser" | Browser tabs only |
"window" | Application windows |
"monitor" | Entire screen/monitor |
System Audio Capture (Chrome)
// Chrome only — capture tab audio when sharing a tab const stream = await navigator.mediaDevices.getDisplayMedia({ video: { displaySurface: 'browser' }, audio: true, // user must check "Share tab audio" in the picker }); const [audioTrack] = stream.getAudioTracks(); if (audioTrack) { console.log('System audio captured:', audioTrack.label); }
Firefox and Safari do not support system audio capture via
getDisplayMedia.
Detecting When the User Stops Sharing
const [track] = screenStream.getVideoTracks(); // Fires when user clicks "Stop Sharing" in the browser bar track.onended = () => { console.log('User stopped screen sharing'); cleanup(); }; // Also check readyState if (track.readyState === 'ended') { // Already stopped }
Reading Track Settings After Capture
const [track] = screenStream.getVideoTracks(); const settings = track.getSettings(); console.log(settings.displaySurface); // 'monitor' | 'window' | 'browser' console.log(settings.width); console.log(settings.height); console.log(settings.frameRate); console.log(settings.logicalSurface); // boolean — is the full logical surface captured? console.log(settings.cursor); // 'always' | 'motion' | 'never'
Common Screen Sharing Gotchas
- No gesture =
InvalidStateError—getDisplayMediathrows if not triggered by a user event. - User can stop sharing at any time from the browser's "Stop sharing" bar — always handle
track.onended. - Screen content may change resolution when the user resizes a window — the track settings update dynamically; listen for constraint changes.
- Safari supports
getDisplayMediafrom Safari 13 (macOS) but not on iOS (as of 2024 iOS 18 remains restricted). - Firefox does not support system audio capture.
preferCurrentTab: true(Chrome 107+) pre-highlights the current tab in the picker but the user can still choose another surface.- High CPU on capture — screen capture at 60 fps is expensive; use
frameRate: { max: 15 }for slide sharing,frameRate: { ideal: 60 }only for game/video streaming. replaceTrackvs renegotiation — switching from camera to screen track withreplaceTrackavoids a new offer/answer cycle, but both tracks must be the samekind(video).