MediaTrackConstraints
TheMediaTrackConstraints dictionary is used to describe a set of media capabilities and the value or values each can take on.
A constraints dictionary is passed into theapplyConstraints() method of theMediaStreamTrack interface to allow a script to establish a set of exact (required) values or ranges and/or preferred values or ranges of values for the track.
The most recently-requested set of custom constraints can be retrieved by callinggetConstraints().
Objects of this type may also be passed to:
The
MediaDevices.getUserMedia()method, to specify constraints on a media stream requested from hardware such as a camera or microphone.The
MediaDevices.getDisplayMedia()method, to specify constraints on a media stream requested from a screen or window capture.
In this article
Constraints
The following types are used to specify a constraint for a property.They allow you to specify one or moreexact values from which one must be the parameter's value, or a set ofideal values which should be used if possible.You can also specify a single value (or an array of values) which the user agent will do its best to match once all more stringent constraints have been applied.
To learn more about how constraints work, seeCapabilities, constraints, and settings.
Note:min andexact values are not permitted in constraints used inMediaDevices.getDisplayMedia() calls — they produce aTypeError — but they are allowed in constraints used inMediaStreamTrack.applyConstraints() calls.
ConstrainBoolean
TheConstrainBoolean constraint type is used to specify a constraint for a property whose value is a Boolean value.Its value may either be set to a Boolean (true orfalse) or an object containing the following properties:
ConstrainBooleanOrDOMString
TheConstrainBooleanOrDOMString constraint type is used to specify a constraint for a property whose value is a Boolean or string value. It can take values as specified in theConstrainBoolean andConstrainDOMString sections.
ConstrainDouble
TheConstrainDouble constraint type is used to specify a constraint for a property whose value is a double-precision floating-point number.Its value may either be set to a number or an object containing the following properties:
maxA decimal number specifying the largest permissible value of the property it describes.If the value cannot remain equal to or less than this value, matching will fail.
minA decimal number specifying the smallest permissible value of the property it describes.If the value cannot remain equal to or greater than this value, matching will fail.
exactA decimal number specifying a specific, required, value the property must have to be considered acceptable.
idealA decimal number specifying an ideal value for the property.If possible, this value will be used, but if it's not possible, the user agent will use the closest possible match.
ConstrainDOMString
TheConstrainDOMString constraint type is used to specify a constraint for a property whose value is a string.Its value may either be set to a string, an array of strings, or an object containing the following properties:
exactA string or an array of strings, one of which must be the value of the property.If the property can't be set to one of the listed values, matching will fail.
idealA string or an array of strings, specifying ideal values for the property.If possible, one of the listed values will be used, but if it's not possible, the user agent will use the closest possible match.
ConstrainULong
TheConstrainULong constraint type is used to specify a constraint for a property whose value is an integer.Its value may either be set to a number or an object containing the following properties:
maxAn integer specifying the largest permissible value of the property it describes.If the value cannot remain equal to or less than this value, matching will fail.
minAn integer specifying the smallest permissible value of the property it describes.If the value cannot remain equal to or greater than this value, matching will fail.
exactAn integer specifying a specific, required, value the property must have to be considered acceptable.
idealAn integer specifying an ideal value for the property.If possible, this value will be used, but if it's not possible, the user agent will use the closest possible match.
Instance properties
Some combination—but not necessarily all—of the following properties will exist on the object.This may be because a given browser doesn't support the property, or because it doesn't applyFor example, becauseRTP doesn't provide some of these values during negotiation of a WebRTC connection, a track associated with aRTCPeerConnection will not include certain values, such asfacingMode orgroupId.
Instance properties of all media tracks
deviceIdA
ConstrainDOMStringobject specifying a device ID or an array of device IDs which are acceptable and/or required.groupIdA
ConstrainDOMStringobject specifying a group ID or an array of group IDs which are acceptable and/or required.
Instance properties of audio tracks
autoGainControlA
ConstrainBooleanobject which specifies whether automatic gain control is preferred and/or required.channelCountA
ConstrainULongspecifying the channel count or range of channel counts which are acceptable and/or required.echoCancellationA
ConstrainBooleanOrDOMStringobject specifying whether or not echo cancellation is preferred and/or required, and if supported, what type.latencyA
ConstrainDoublespecifying the latency or range of latencies which are acceptable and/or required.noiseSuppressionA
ConstrainBooleanwhich specifies whether noise suppression is preferred and/or required.sampleRateA
ConstrainULongspecifying the sample rate or range of sample rates which are acceptable and/or required.sampleSizeA
ConstrainULongspecifying the sample size or range of sample sizes which are acceptable and/or required.volumeDeprecatedNon-standardA
ConstrainDoublespecifying the volume or range of volumes which are acceptable and/or required.
Instance properties of image tracks
whiteBalanceModeA
Stringspecifying one of"none","manual","single-shot", or"continuous".exposureModeA
Stringspecifying one of"none","manual","single-shot", or"continuous".focusModeA
Stringspecifying one of"none","manual","single-shot", or"continuous".pointsOfInterestThe pixel coordinates on the sensor of one or more points of interest.This is either an object in the form { x:value, y:value } or an array of such objects, wherevalue is a double-precision integer.
exposureCompensationA
ConstrainDouble(a double-precision integer) specifying f-stop adjustment by up to ±3.colorTemperatureA
ConstrainDouble(a double-precision integer) specifying a desired color temperature in degrees kelvin.isoA
ConstrainDouble(a double-precision integer) specifying a desired iso setting.brightnessA
ConstrainDouble(a double-precision integer) specifying a desired brightness setting.contrastA
ConstrainDouble(a double-precision integer) specifying the degree of difference between light and dark.saturationA
ConstrainDouble(a double-precision integer) specifying the degree of color intensity.sharpnessA
ConstrainDouble(a double-precision integer) specifying the intensity of edges.focusDistanceA
ConstrainDouble(a double-precision integer) specifying distance to a focused object.zoomA
ConstrainDouble(a double-precision integer) specifying the desired focal length.torchA boolean value defining whether the fill light is continuously connected, meaning it stays on as long as the track is active.
Instance properties of video tracks
aspectRatioA
ConstrainDoublespecifying the videoaspect ratio or range of aspect ratios which are acceptable and/or required.facingModeA
ConstrainDOMStringobject specifying a facing or an array of facings which are acceptable and/or required.frameRateA
ConstrainDoublespecifying the frame rate or range of frame rates which are acceptable and/or required.heightA
ConstrainULongspecifying the video height or range of heights which are acceptable and/or required.widthA
ConstrainULongspecifying the video width or range of widths which are acceptable and/or required.resizeModeA
ConstrainDOMStringobject specifying a mode or an array of modes the UA can use to derive the resolution and frame rate of a video track.Allowed values are:crop-and-scaleThe user agent can use cropping and downscaling of resolution or frame rate on the raw output from the hardware/OS, in order to satisfy other constraints.This constraint allows developers to get a downscaled video even if the particular format indicated by their constraints is not natively supported by the hardware.
noneThe user agent uses the resolution provided by the underlying hardware, such as a camera or its driver, or the OS.
If
resizeModeis unspecified the browser will choose a resolution based on afitness distance that considers the specified constraints andboth of the allowed values.
Instance properties of shared screen tracks
These constraints apply to thevideo property of the object passed intogetDisplayMedia() to obtain a stream for screen sharing.
displaySurfaceA
ConstrainDOMStringwhich specifies the types of display surface that may be selected by the user.This may be a single one of the following strings, or a list of them to allow multiple source surfaces:logicalSurfaceA
ConstrainBooleanvalue which may contain a single Boolean value or a set of them, indicating whether or not to allow the user to choose source surfaces which do not directly correspond to display areas.These may include backing buffers for windows to allow capture of window contents that are hidden by other windows in front of them, or buffers containing larger documents that need to be scrolled through to see the entire contents in their windows.suppressLocalAudioPlaybackExperimentalA
ConstrainBooleanvalue describing the requested or mandatory constraints placed upon the value of thesuppressLocalAudioPlaybackconstrainable property.This property controls whether the audio playing in a tab will continue to be played out of a user's local speakers when the tab is captured.restrictOwnAudioExperimentalA
ConstrainBooleanvalue that specifies the requested or mandatory constraints placed on the value of therestrictOwnAudioconstrainable property.This property controls whether the system audio originating from the capturing tab is filtered out of the screen capture.
Specifications
| Specification |
|---|
| Media Capture and Streams> # dom-mediatrackconstraints> |
| Screen Capture> # extensions-to-mediatrackconstraintset> |
See also
- Media Capture and Streams API
- Capabilities, constraints, and settings
- Screen Capture API
- Using the Screen Capture API
MediaStreamTrack.getConstraints()MediaStreamTrack.applyConstraints()MediaDevices.getUserMedia()MediaDevices.getDisplayMedia()MediaDevices.getSupportedConstraints()MediaTrackSupportedConstraintsMediaStreamTrack.getSettings()