Skip to content

Commit

Permalink
Merge pull request #25 from PierreFritsch/patch-2
Browse files Browse the repository at this point in the history
step-05: Remove dead code: Function preferOpus never used

Apologies for the long delay in merging! 

I agree with your suggesting to remove the unused code.
  • Loading branch information
samdutton authored Mar 8, 2018
2 parents ea50d9b + ab00edb commit bcb772a
Showing 1 changed file with 0 additions and 80 deletions.
80 changes: 0 additions & 80 deletions step-05/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@ function doAnswer() {
}

function setLocalAndSendMessage(sessionDescription) {
// Set Opus as the preferred codec in SDP if Opus is present.
// sessionDescription.sdp = preferOpus(sessionDescription.sdp);
pc.setLocalDescription(sessionDescription);
console.log('setLocalAndSendMessage sending message', sessionDescription);
sendMessage(sessionDescription);
Expand Down Expand Up @@ -257,81 +255,3 @@ function stop() {
pc.close();
pc = null;
}

///////////////////////////////////////////

// Set Opus as the default audio codec if it's present.
function preferOpus(sdp) {
var sdpLines = sdp.split('\r\n');
var mLineIndex;
// Search for m line.
for (var i = 0; i < sdpLines.length; i++) {
if (sdpLines[i].search('m=audio') !== -1) {
mLineIndex = i;
break;
}
}
if (mLineIndex === null) {
return sdp;
}

// If Opus is available, set it as the default in m line.
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;
}
}

// Remove CN in m line and sdp.
sdpLines = removeCN(sdpLines, mLineIndex);

sdp = sdpLines.join('\r\n');
return sdp;
}

function extractSdp(sdpLine, pattern) {
var result = sdpLine.match(pattern);
return result && result.length === 2 ? result[1] : null;
}

// Set the selected codec to the first in m line.
function setDefaultCodec(mLine, payload) {
var elements = mLine.split(' ');
var newLine = [];
var index = 0;
for (var i = 0; i < elements.length; i++) {
if (index === 3) { // Format of media starts from the fourth.
newLine[index++] = payload; // Put target payload to the first.
}
if (elements[i] !== payload) {
newLine[index++] = elements[i];
}
}
return newLine.join(' ');
}

// Strip CN from sdp before CN constraints is ready.
function removeCN(sdpLines, mLineIndex) {
var mLineElements = sdpLines[mLineIndex].split(' ');
// Scan from end for the convenience of removing an item.
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) {
// Remove CN payload from m line.
mLineElements.splice(cnPos, 1);
}
// Remove CN line in sdp
sdpLines.splice(i, 1);
}
}

sdpLines[mLineIndex] = mLineElements.join(' ');
return sdpLines;
}

0 comments on commit bcb772a

Please sign in to comment.