Created
November 5, 2017 06:43
-
-
Save Nazmul56/c7992bcc763090609e8e40c2eda86d00 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var prioritizeVideoCodecs; | |
var prioritizeAudioCodecs; | |
//On peer Connection | |
this.pc.on('offer', function (offer) { | |
offer = prioritizeVideoCodecs(offer); | |
offer = prioritizeAudioCodecs(offer); | |
if (self.parent.config.nick) offer.nick = self.parent.config.nick; | |
self.send('offer', offer); | |
}); | |
prioritizeVideoCodecs = function(payload) { | |
var priority = ["VP9","VP8","H.264","H.264"]; // ordered priority list, first = highest priority | |
var ids = []; | |
Object.keys(priority).forEach(function (key) { | |
var id = findCodecId(payload, priority[key]); | |
if (id) { | |
ids.push(id); | |
} | |
}); | |
if (ids.length > 0 && payload && payload.sdp) { | |
var sdp = payload.sdp; | |
var m = sdp.match(/m=video\s(\d+)\s[A-Z\/]+\s([0-9\ ]+)/); | |
if (m !== null && m.length === 3) { | |
var candidates = m[2].split(" "); | |
var prioritized = ids; | |
Object.keys(candidates).forEach(function (key) { | |
if (ids.indexOf(candidates[key]) === -1) { | |
//prioritized.push(121); | |
prioritized.push(candidates[key]); | |
} | |
}); | |
var mPrioritized = m[0].replace(m[2], prioritized.join(" ")); | |
// logger.info("Setting video codec priority. \"%s\"", mPrioritized); | |
payload.sdp = sdp.replace(m[0], mPrioritized); | |
} | |
} | |
return payload; | |
}; | |
prioritizeAudioCodecs = function(payload) { | |
// logger.info(payload); | |
// ,"telephone-event" "opus" | |
var priority = ["iLBC","PCMU","opus","telephone-event"]; | |
var ids = []; | |
Object.keys(priority).forEach(function (key) { | |
var id = findCodecId(payload, priority[key]); | |
if (id) { | |
ids.push(id); | |
} | |
}); | |
if (ids.length > 0 && payload && payload.sdp) { | |
var sdp = payload.sdp; | |
var m = sdp.match(/m=audio\s(\d+)\s[A-Z\/]+\s([0-9\ ]+)/); | |
if (m !== null && m.length === 3) { | |
var candidates = m[2].split(" "); | |
var prioritized = ids; | |
Object.keys(candidates).forEach(function (key) { | |
if (ids.indexOf(candidates[key]) === -1) { | |
//prioritized.push(121); | |
prioritized.push(candidates[key]); | |
} | |
}); | |
var mPrioritized = m[0].replace(m[2], prioritized.join(" ")); | |
// logger.info("audio codec Details: . \"%s\"", mPrioritized); | |
//When Need To Modifiy Priority | |
payload.sdp = sdp.replace(m[0], mPrioritized); | |
} | |
} | |
/* var maxAvgBitRate = config.maxAverageBitRate || 0; | |
if (maxAvgBitRate > 0) { | |
var id = findCodecId(details, "opus"); | |
if (id && details.payload && details.payload.sdp) { | |
details.payload.sdp = alterFmtpConfig(details.payload.sdp, id, {"maxaveragebitrate": maxAvgBitRate}); | |
} | |
}*/ | |
return payload; | |
}; | |
function findCodecId(details, codec) { | |
if (details.payload && details.payload.sdp) { | |
var pattern = "a=rtpmap\\:(\\d+)\\s" + codec + "\\/\\d+"; | |
var re = new RegExp(pattern); | |
var m = details.payload.sdp.match(re); | |
if (m !== null && m.length > 0) { | |
return m[1]; | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment