files/en-us/web/api/rtcrtpsender/setparameters/index.md
{{APIRef("WebRTC API")}}
The setParameters() method of the {{domxref("RTCRtpSender")}} interface applies changes the configuration of sender's {{domxref("RTCRtpSender.track", "track")}}, which is the {{domxref("MediaStreamTrack")}} for which the RTCRtpSender is responsible.
In other words, setParameters() updates the configuration of the {{Glossary("RTP")}} transmission as well as the encoding configuration for a specific outgoing media track on the WebRTC connection.
setParameters(parameters)
parameters
encodings
active
true (the default) causes this encoding to be sent, while false stops it from being sent and used (but does not cause the SSRC to be removed).codec {{optional_inline}}
channels {{optional_inline}}
clockRate
mimeType
"type/subtype".
The MIME type strings used by RTP differ from those used elsewhere.
IANA maintains a registry of valid MIME types.
Also see Codecs used by WebRTC for details about potential codecs that might be referenced here.sdpFmtpLine {{optional_inline}}
dtx {{Deprecated_Inline}} {{Non-standard_Inline}}
audio, this property indicates whether or not to use discontinuous transmission (a feature by which a phone is turned off or the microphone muted automatically in the absence of voice activity).
The value is taken either enabled or disabled.maxBitrate
: A positive integer indicating the maximum number of bits per second that the user agent is allowed to grant to tracks encoded with this encoding.
Other parameters may further constrain the bit rate, such as the value of maxFramerate, or the bandwidth available for the transport or physical network.
The value is computed using the standard Transport Independent Application Specific Maximum (TIAS) bandwidth as defined by {{RFC(3890, "", "6.2.2")}}; this is the maximum bandwidth needed without considering protocol overheads from IP, TCP or UDP, and so forth.
Note that the bitrate can be achieved in a number of ways, depending on the media and encoding. For example, for video a low bit rate might be achieved by dropping frames (a bitrate of zero might allow just one frame to be sent), while for audio the track might have to stop playing if the bitrate is too low for it to be sent.
maxFramerate
priority
very-low, low (default), medium, high.rid
setParameters().
Its value can only be set when the transceiver is first created.scaleResolutionDownBy
video, this is a floating-point value specifying a factor by which to scale down the video during encoding.
The default value, 1.0, means that the video will be encoded at its original size.
A value of 2.0 scales the video frames down by a factor of 2 in each dimension, resulting in a video 1/4 the size of the original.
The value must not be less than 1.0 (attempting to scale the video to a larger size will throw a {{jsxref("RangeError")}}).transactionId
codecs
: An array of objects describing the media codecs from which the sender will choose. This parameter cannot be changed once initially set.
Each codec object in the array may have the following properties: <!-- RTCRtpCodecParameters -->
channels {{optional_inline}}
clockRate
mimeType
"type/subtype".
The MIME type strings used by RTP differ from those used elsewhere.
IANA maintains a registry of valid MIME types.
Also see Codecs used by WebRTC for details about potential codecs that might be referenced here.payloadType
sdpFmtpLine {{optional_inline}}
headerExtensions
rtcp
: An object providing the configuration parameters used for {{Glossary("RTCP")}} on the sender. This parameter cannot be changed.
The object may have the following properties: <!-- RTCRtcpParameters -->
cname
reducedSize
True if reduced size RTCP is configured ({{rfc("5506")}}), and False if compound RTCP is specified ({{rfc("3550")}}).degradationPreference {{optional_inline}}
balanced
maintain-framerate
maintain-resolution
maintain-framerate-and-resolution
A {{jsxref("Promise")}} that resolves when the {{domxref("RTCRtpSender.track")}} property is updated with the given parameters.
If an error occurs, the returned promise is rejected with the appropriate exception from the list below.
InvalidModificationError {{domxref("DOMException")}}
parameters object's encodings property does not match the number of encodings currently listed for the RTCRtpSender.
You cannot change the number of encoding options after the sender has been created.encodings has changed from the current list's order.InvalidStateError {{domxref("DOMException")}}
RTCRtpSender is a part, is not running or has no parameters to set.OperationError {{domxref("DOMException")}}
scaleResolutionDownBy option is less than 1.0 — which would result in scaling up rather than down, which is not allowed; or if one or more of the specified encodings maxFramerate values is less than 0.0.In addition, if a WebRTC error occurs while configuring or accessing the media, an {{domxref("RTCError")}} is thrown with its {{domxref("RTCError.errorDetail", "errorDetail")}} set to hardware-encoder-error.
It's important to keep in mind that you can't create the parameters object yourself and expect it to work.
Instead, you must first call {{domxref("RTCRtpSender.getParameters", "getParameters()")}}, modify the received parameters object, then pass that object into setParameters().
WebRTC uses the parameters object's transactionId property to ensure that when you set parameters, your changes are based on the most recent parameters rather than an out of date configuration.
One use case for setParameters() is to try to reduce network bandwidth used in constrained environments by altering the resolution and/or bit rate of the media being transmitted by the {{domxref("RTCRtpSender")}}.
Currently, some browsers have limitations on their implementations that may cause issues.
For that reason, two examples are given here.
The first shows how to use setParameters() when all browsers fully support the parameters being used, while the second example demonstrates workarounds to help solve limitations in browsers with incomplete support for the maxBitrate and scaleResolutionDownBy parameters.
Once all browsers implement the spec fully, this implementation of setVideoParams() will do the job. This demonstrates how everything should work.
You should probably use the second example, below, for now.
But this is a clearer demonstration of the basic concept of first fetching the parameters, then altering them, then setting them.
async function setVideoParams(sender, height, bitrate) {
const scaleRatio = sender.track.getSettings().height / height;
const params = sender.getParameters();
params.encodings[0].scaleResolutionDownBy = Math.max(scaleRatio, 1);
params.encodings[0].maxBitrate = bitrate;
await sender.setParameters(params);
}
In calling this function, you specify a sender, as well as the height you wish to scale the sender's video to, as well as a maximum bitrate to permit the sender to transmit.
A scaling factor for the size of the video, scaleRatio, is computed.
Then the sender's current parameters are fetched using {{domxref("RTCRtpSender.getParameters", "getParameters()")}}.
The parameters are then altered by changing the first encodings object's scaleResolutionDownBy and maxBitrate to the calculated scaling factor and the specified maximum bitrate.
The changed parameters are then saved by calling the sender's setParameters() method.
As mentioned above, the previous example shows how things are meant to work. Unfortunately, there are implementation issues preventing this in many browsers right now. For that reason, if you want to be compatible with iPhone and other devices running Safari, and with Firefox, use code more like this:
async function setVideoParams(sender, height, bitrate) {
const scaleRatio = sender.track.getSettings().height / height;
const params = sender.getParameters();
// If encodings is null, create it
params.encodings ??= [{}];
params.encodings[0].scaleResolutionDownBy = Math.max(scaleRatio, 1);
params.encodings[0].maxBitrate = bitrate;
await sender.setParameters(params);
// If the newly changed value of scaleResolutionDownBy is 1,
// use applyConstraints() to be sure the height is constrained,
// since scaleResolutionDownBy may not be implemented
if (sender.getParameters().encodings[0].scaleResolutionDownBy === 1) {
await sender.track.applyConstraints({ height });
}
}
The differences here:
encodings is null, we create it, in order to ensure that we can then set the parameters successfully without crashing.scaleResolutionDownBy is still 1, we call the sender's track's {{domxref("MediaStreamTrack.applyConstraints", "applyConstraints()")}} method to constrain the track's height to height.
This compensates for an unimplemented scaleResolutionDownBy (as is the case in Safari as of this writing).This code will cleanly fall back and work the normal way if the browser fully implements the used features.
{{Specifications}}
{{Compat}}