Skip to content

Instantly share code, notes, and snippets.

@Nazmul56
Created January 16, 2018 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nazmul56/13898ee156270671fda30904c575c23d to your computer and use it in GitHub Desktop.
Save Nazmul56/13898ee156270671fda30904c575c23d to your computer and use it in GitHub Desktop.
// Helper method to munge an SDP to enable simulcasting (Chrome only)
function mungeSdpForSimulcasting(sdp) {
// Let's munge the SDP to add the attributes for enabling simulcasting
// (based on https://gist.github.com/ggarber/a19b4c33510028b9c657)
var lines = sdp.split("\r\n");
var video = false;
var ssrc = [-1],
ssrc_fid = -1;
var cname = null,
msid = null,
mslabel = null,
label = null;
var insertAt = -1;
for (var i = 0; i < lines.length; i++) {
var mline = lines[i].match(/m=(\w+) */);
if (mline) {
var medium = mline[1];
if (medium === "video") {
// New video m-line: make sure it's the first one
if (ssrc[0] < 0) {
video = true;
} else {
// We're done, let's add the new attributes here
insertAt = i;
break;
}
} else {
// New non-video m-line: do we have what we were looking for?
if (ssrc[0] > -1) {
// We're done, let's add the new attributes here
insertAt = i;
break;
}
}
continue;
}
if (!video) continue;
var fid = lines[i].match(/a=ssrc-group:FID (\d+) (\d+)/);
if (fid) {
ssrc[0] = fid[1];
ssrc_fid = fid[2];
lines.splice(i, 1);i--;
continue;
}
if (ssrc[0]) {
var match = lines[i].match('a=ssrc:' + ssrc[0] + ' cname:(.+)');
if (match) {
cname = match[1];
}
match = lines[i].match('a=ssrc:' + ssrc[0] + ' msid:(.+)');
if (match) {
msid = match[1];
}
match = lines[i].match('a=ssrc:' + ssrc[0] + ' mslabel:(.+)');
if (match) {
mslabel = match[1];
}
match = lines[i].match('a=ssrc:' + ssrc + ' label:(.+)');
if (match) {
label = match[1];
}
if (lines[i].indexOf('a=ssrc:' + ssrc_fid) === 0) {
lines.splice(i, 1);i--;
continue;
}
if (lines[i].indexOf('a=ssrc:' + ssrc[0]) === 0) {
lines.splice(i, 1);i--;
continue;
}
}
if (lines[i].length == 0) {
lines.splice(i, 1);i--;
continue;
}
}
if (ssrc[0] < 0) {
// Couldn't find a FID attribute, let's just take the first video SSRC we find
insertAt = -1;
video = false;
for (var i = 0; i < lines.length; i++) {
var mline = lines[i].match(/m=(\w+) */);
if (mline) {
var medium = mline[1];
if (medium === "video") {
// New video m-line: make sure it's the first one
if (ssrc[0] < 0) {
video = true;
} else {
// We're done, let's add the new attributes here
insertAt = i;
break;
}
} else {
// New non-video m-line: do we have what we were looking for?
if (ssrc[0] > -1) {
// We're done, let's add the new attributes here
insertAt = i;
break;
}
}
continue;
}
if (!video) continue;
if (ssrc[0] < 0) {
var value = lines[i].match(/a=ssrc:(\d+)/);
if (value) {
ssrc[0] = value[1];
lines.splice(i, 1);i--;
continue;
}
} else {
var match = lines[i].match('a=ssrc:' + ssrc[0] + ' cname:(.+)');
if (match) {
cname = match[1];
}
match = lines[i].match('a=ssrc:' + ssrc[0] + ' msid:(.+)');
if (match) {
msid = match[1];
}
match = lines[i].match('a=ssrc:' + ssrc[0] + ' mslabel:(.+)');
if (match) {
mslabel = match[1];
}
match = lines[i].match('a=ssrc:' + ssrc + ' label:(.+)');
if (match) {
label = match[1];
}
if (lines[i].indexOf('a=ssrc:' + ssrc_fid) === 0) {
lines.splice(i, 1);i--;
continue;
}
if (lines[i].indexOf('a=ssrc:' + ssrc[0]) === 0) {
lines.splice(i, 1);i--;
continue;
}
}
if (lines[i].length == 0) {
lines.splice(i, 1);i--;
continue;
}
}
}
if (ssrc[0] < 0) {
// Still nothing, let's just return the SDP we were asked to munge
Janus.warn("Couldn't find the video SSRC, simulcasting NOT enabled");
return sdp;
}
if (insertAt < 0) {
// Append at the end
insertAt = lines.length;
}
// Generate a couple of SSRCs
ssrc[1] = Math.floor(Math.random() * 0xFFFFFFFF);
ssrc[2] = Math.floor(Math.random() * 0xFFFFFFFF);
// Add attributes to the SDP
for (var i = 0; i < ssrc.length; i++) {
if (cname) {
lines.splice(insertAt, 0, 'a=ssrc:' + ssrc[i] + ' cname:' + cname);
insertAt++;
}
if (msid) {
lines.splice(insertAt, 0, 'a=ssrc:' + ssrc[i] + ' msid:' + msid);
insertAt++;
}
if (mslabel) {
lines.splice(insertAt, 0, 'a=ssrc:' + ssrc[i] + ' mslabel:' + msid);
insertAt++;
}
if (label) {
lines.splice(insertAt, 0, 'a=ssrc:' + ssrc[i] + ' label:' + msid);
insertAt++;
}
}
lines.splice(insertAt, 0, 'a=ssrc-group:SIM ' + ssrc[0] + ' ' + ssrc[1] + ' ' + ssrc[2]);
sdp = lines.join("\r\n");
if (!sdp.endsWith("\r\n")) sdp += "\r\n";
return sdp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment