Hey I would like to know if there is any way to choose codecs when creating the offer/answer in WebRTC. There are currently not many video codecs to choose from, but there are audio codecs like Opus, PCMU, PCMA etc.
What codecs are supported in WebRTC? The currently supported voice codecs are G. 711, G. 722, iLBC, and iSAC, and VP8 is the supported video codec.
AVC / H. 264. Support for AVC's Constrained Baseline (CB) profile is required in all fully-compliant WebRTC implementations.
VP8 is a royalty free video compression format (also known as a video codec). VP8 was open sourced by Google and is being used as the current default implementation of video codec in WebRTC. VP8 is available in all browsers that support WebRTC. It is one of the mandatory to implement video codecs in WebRTC.
However, if your streams contain AAC/Opus audio or VP8/VP9 video, then transcoding is needed, because RTMP doesn't support Opus audio and VP8/VP9 video; WebRTC doesn't support AAC audio.
In general, yes. Here is example of how to prefer Opus codec during establishing conneciton. You should call 'preferOpus' from a callback function for createAnswer or createOffer.
var preferOpus = function(sdp) {
var sdpLines = sdp.split('\r\n');
for (var i = 0; i < sdpLines.length; i++) {
if (sdpLines[i].search('m=audio') !== -1) {
var mLineIndex = i;
break;
}
}
if (mLineIndex === null) return sdp;
for (i = 0; i < sdpLines.length; i++) {
if (sdpLines[i].search('opus/48000') !== -1) {
var opusPayload = extractSdp(sdpLines[i], /:(\d+) opus\/48000/i);
if (opusPayload)
sdpLines[mLineIndex] = setDefaultCodec(sdpLines[mLineIndex], opusPayload);
break;
}
}
sdpLines = removeCN(sdpLines, mLineIndex);
sdp = sdpLines.join('\r\n');
return sdp;
};
var extractSdp = function(sdpLine, pattern) {
var result = sdpLine.match(pattern);
return (result && result.length == 2)? result[1]: null;
};
var setDefaultCodec = function(mLine, payload) {
var elements = mLine.split(' ');
var newLine = new Array();
var index = 0;
for (var i = 0; i < elements.length; i++) {
if (index === 3) newLine[index++] = payload;
if (elements[i] !== payload) newLine[index++] = elements[i];
}
return newLine.join(' ');
};
var removeCN = function(sdpLines, mLineIndex) {
var mLineElements = sdpLines[mLineIndex].split(' ');
for (var i = sdpLines.length-1; i >= 0; i--) {
var payload = extractSdp(sdpLines[i], /a=rtpmap:(\d+) CN\/\d+/i);
if (payload) {
var cnPos = mLineElements.indexOf(payload);
if (cnPos !== -1) mLineElements.splice(cnPos, 1);
sdpLines.splice(i, 1);
}
}
sdpLines[mLineIndex] = mLineElements.join(' ');
return sdpLines;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With