WebRTC is great at negotiating a mediastream and peer connection, but sometimes abstracts the codec used. When you want to use a specific codec, you need to modify the default session description. In the session description (SDP) there exist lines such as:
m=video 9 RTP/SAVPF 120 126 97
....
a=rtpmap:97 H264/90000
In the sdp, the m= specifies the media description type (i.e video), while the subsequent numbers specify the codecs (i.e 120, 126, 97). In the above example, the numeric value 97 maps to the a= entry for H264, indicating it is the third preferred codec behind 120 & 126. The order of the codecs determines the preference in which they will be used, depending on the shared set of codecs between RTC peers. Therefore to specify a specific codec, you need to alter the SDP and put your preferred codecs at the beginning of the numeric list in the m= entry. While you could try to do this manually by modifying the offer.sdp after it has been created but before it is sent to the peer, it is easier to use the browser standard RTCRtpTransceiver.setCodecPreferences that most browsers support. This allows you to pass a list of RTCRtpCodecCapability objects to specify the codecs you would like to prioritize at the beginning of the SDP offer. The easiest way to get these codec capability objects is to query the browser for the list of supported codecs, and to filter out the codec you want.
How to force WebRTC to use VP9 codec?
In the following javascript client example, we will specify the vp9 codec for WebRTC, using the general method described above.
Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!
We can use this same method to set audio codecs, just change the getCapabilities(‘video’) to getCapabilities(‘audio’) and change the mimeType to your codec of choice.
Elevate your software skills
Ergonomic Mouse |
Custom Keyboard |
SW Architecture |
Clean Code |