I've been googling a way to change codec in Chrome's implementation of WebRTC, but there doesn't seem to be a way.
How can I change the default codec used(audio or video) in a WebRTCpeer connection in Chrome?
As browsers start to support setCodecPreferences, you can check for the mimetype of the codec you want to use by default to set the codec preference. For example if you want to prefer opus for audio you can check for the "audio/opus" mimetype and set your codec preferences to opus codecs:
let tcvr = pc.getTransceivers()[0];
let codecs = RTCRtpReceiver.getCapabilities('audio').codecs;
let opus_codecs = [];
// iterate over supported codecs and pull out the codecs we want
for(let i = 0; i < codecs.length; i++)
{
if(codecs[i].mimeType == "audio/opus")
{
opus_codecs .push(codecs[i]);
}
}
// currently not all browsers support setCodecPreferences
if(tcvr.setCodecPreferences != undefined)
{
tcvr.setCodecPreferences(opus_codecs);
}
Or for video you can fix the codec to vp9:
// note the following should be called before before calling either RTCPeerConnection.createOffer() or createAnswer()
let tcvr = pc.getTransceivers()[0];
let codecs = RTCRtpReceiver.getCapabilities('video').codecs;
let vp9_codecs = [];
// iterate over supported codecs and pull out the codecs we want
for(let i = 0; i < codecs.length; i++)
{
if(codecs[i].mimeType == "video/VP9")
{
vp9_codecs.push(codecs[i]);
}
}
// currently not all browsers support setCodecPreferences
if(tcvr.setCodecPreferences != undefined)
{
tcvr.setCodecPreferences(vp9_codecs);
}
Code adapted from this Pericror blog post to force audio/video codecs.
Yes, you can change the codec to be anything you want...as long as Chrome supports it. Right now, audio wise, the only supported codecs are PCMA, PCMU, ISAC, and OPUS(the default). For Video you have VP8(also H264 on some systems with FireFox).
To use any of these codecs as default, you must modify your SDP before setting it locally in your peerconnection and sending your offer/answer. I have tested successfully forcing Chrome to send PCMA instead of OPUS by default.
As an example:
Say you have your default audio SDP section that looks like the following(notes are in brackets are are not part of the sdp)
If you wanted to ONLY use PCMA, you would change the
m=audio
line to the following:m=audio 49353 RTP/SAVPF 8
this way only the PCMA payload is considered. Then you need to remove all the rtpmap lines that do not correspond with that payload, i.e. anya=rtpmap:
where the next character is NOT an 8. If you set that modified sdp locally and send it to your peer(and if they SUPPORT PCMA...does not have to be default for them as the negotiation will force PCMA if you only offer it), then PCMA will be your audio codec and not OPUS.Couple of asides:
createOffer
andcreateAnswer
functions of the peerconnectionfmtp
is needed for h264 as profiles are important and possiblysprop-parameter-sets
).