diff --git a/step-05/js/main.js b/step-05/js/main.js index 51b3fa3..8c60d51 100644 --- a/step-05/js/main.js +++ b/step-05/js/main.js @@ -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); @@ -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; -}