The RTCRtpTransceiver
method setCodecPreferences()
configures the transceiver's preferred list of codecs.
The specified set of codecs will be used for all future connections that include this transceiver until this method is called again.
When preparing to open an RTCPeerConnection
, you can change the codec parameters from the user agent's default configuration by calling setCodecParameters()
before calling either RTCPeerConnection.createOffer()
or createAnswer()
.
A guide to codecs supported by WebRTC—and each codec's positive and negative characteristics—can be found in Codecs used by WebRTC.
setCodecPreferences(codecs)
You can only include in the codecs
list codecs which the transceiver actually supports. That means that either the associated RTCRtpSender
or the RTCRtpReceiver
needs to support every codec in the list. If any unsupported codecs are listed, the browser will throw an InvalidAccessError
exception when you call this method.
A good approach to setting codec preferences is to first get the list of codecs that are actually supported, then modify that list to match what you want. Pass the altered list into setCodecPreferences()
to specify your preferences.
To determine which codecs are supported by the transceiver, call the sender's getCapabilities()
and the receiver's getCapabilities()
methods and get the codecs
array from the results of each.
The following code snippet demonstrates how to get both the list of codecs supported by the transceiver's RTCRtpSender
and RTCRtpReceiver
.
const availSendCodecs = transceiver.sender.getCapabilities("video").codecs;
const availReceiveCodecs = transceiver.receiver.getCapabilities("video").codecs;