The getDisplayMedia() method of the MediaDevices interface prompts the user to select and
grant permission to capture the contents of a display or portion thereof (such as a window) as a MediaStream.
The resulting stream can then be
recorded using the WebRTC session.
An optional object specifying requirements for the returned MediaDevices.getUserMedia() method, although in that case only audio and video can be specified. The list of possible option properties for getDisplayMedia() is as follows:
A boolean or a MediaStream will contain a video track. Since getDisplayMedia() requires a video track, if this option is set to false the promise will reject with a TypeError.
An enumerated value specifying whether the browser should offer entire screens in the screen capture options presented to the user alongside tab and window options. This option is intended to protect companies from leakage of private information through employee error when using video conferencing apps. Possible values are include, which hints that the browser should include screen options, and exclude, which hints that they should be excluded. A default value is not mandated by the spec; see the Browser compatibility section for browser-specific defaults.
Note:
You cannot set monitorTypeSurfaces: "exclude" at the same time as displaySurface: "monitor" as the two settings are contradictory. Trying to do so will result in the getDisplayMedia() call failing with a TypeError.
A boolean; a value of true instructs the browser to offer the current tab as the most prominent capture source, i.e. as a separate "This Tab" option in the "Choose what to share" options presented to the user. This is useful as many app types generally just want to share the current tab. For example, a slide deck app might want to let the user stream the current tab containing the presentation to a virtual conference. A default value is not mandated by the spec; see the Browser compatibility section for browser-specific defaults.
An enumerated value specifying whether the browser should allow the user to select the current tab for capture. This helps to avoid the "infinite hall of mirrors" effect experienced when a video conferencing app inadvertently shares its own display. Possible values are include, which hints that the browser should include the current tab in the choices offered for capture, and exclude, which hints that it should be excluded. A default value is not mandated by the spec; see the Browser compatibility section for browser-specific defaults.
An enumerated value specifying whether the browser should display a control to allow the user to dynamically switch the shared tab during screen-sharing. This is much more convenient than having to go through the whole sharing process again each time a user wants to switch the shared tab. Possible values are include, which hints that the browser should include the control, and exclude, which hints that it should not be shown. A default value is not mandated by the spec; see the Browser compatibility section for browser-specific defaults.
An enumerated value specifying whether the browser should include the system audio among the possible audio sources offered to the user. Possible values are include, which hints that the browser should include the system audio in the list of choices, and exclude, which hints that it should be excluded. A default value is not mandated by the spec; see the Browser compatibility section for browser-specific defaults.
A MediaStream containing a
video track whose contents come from a user-selected screen area, as well as an optional
audio track.
Note:
Browser support for audio tracks varies, both in terms of whether or not they're supported at all by the media recorder and in terms of the audio sources supported. Check the compatibility table for details for each browser.
Thrown if the call to getDisplayMedia() was not made from code running due to a
transient activation, such as an event handler. Or if the browser context is
not fully active or does not focused. Or if the controller options has been already used in creating
another MediaStream.
Thrown if the permission to access a screen area was denied by the user, or the current browsing instance is not permitted access to screen sharing (for example by a Permissions Policy).
Thrown if the user selected a screen, window, tab, or another source of screen data, but a
hardware or operating system level error or lockout occurred, preventing the sharing
of the selected source.
Thrown if the specified options include values that are not permitted
when calling getDisplayMedia(), for example a video property set to false, or if any specified MediaTrackConstraints are not permitted. min and exact values are not permitted in constraints used in getDisplayMedia() calls.
Because getDisplayMedia() could be used in nefarious ways, it can be a
source of significant privacy and security concerns. For that reason, the specification
details measures browsers are required to take in order to fully support
getDisplayMedia().
The specified options can't be used to limit the choices available
to the user. Instead, they must be applied after the user chooses a source, in order
to generate output that matches the options.
The go-ahead permission to use getDisplayMedia() cannot be persisted
for reuse. The user must be prompted for permission every time.
Transient user activation is required. The user has to interact with the page or a UI element in order for this feature to work.
Browsers are encouraged to provide a warning to users about sharing displays or
windows that contain browsers, and to keep a close eye on what other content might be
getting captured and shown to other users.
In the example below a startCapture() method is created, which initiates
screen capture given a set of options specified by the displayMediaOptions
parameter.
This uses await to asynchronously wait for getDisplayMedia()
to resolve with a MediaStream which contains the display contents as
requested by the specified options. The stream is then returned to the caller for use,
perhaps for adding to a WebRTC call using RTCPeerConnection.addTrack() to
add the video track from the stream.
Note:
The Screen sharing controls demo provides a complete implementation that allows you to create a screen capture with your choice of getDisplayMedia() constraints and options.